關於 jsp servlet 將Excel表格內容寫進資料庫,將資料庫匯出到Excel中。(一)

來源:互聯網
上載者:User

轉自:http://blog.chinaunix.net/uid-28794959-id-3773177.html

EXCEL到資料庫,引入poi.jar
jsp如下

點擊(此處)摺疊或開啟 <form enctype="multipart/form-data" name=testform method=post action=Testaction>
  <table>
    <tr>
       <td><font size=2>批量上傳:</font><input type="file" name="test" size="10"><br></td>
      <td><input type="submit" name="批量上傳" size="10"value="批量上傳"><br></td></tr></table><br>
      </form> Servlet如下

點擊(此處)摺疊或開啟 package control;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Testaction extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //接收上傳檔案內容中臨時檔案的檔案名稱
        String tempFileName = new String("tempFileName");
        //tempfile 對象指向臨時檔案
        File tempFile = new File("D:/"+tempFileName);
        //outputfile 檔案輸出資料流指向這個臨時檔案
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        //得到客服端提交的所有資料
        InputStream fileSourcel = request.getInputStream();
        //將得到的客服端資料寫入臨時檔案
        byte b[] = new byte[1000];
        int n ;
        while ((n=fileSourcel.read(b))!=-1){
         outputStream.write(b,0,n);
        }
        
        //關閉輸出資料流和輸入資料流
        outputStream.close();
        fileSourcel.close();
        
        //randomFile對象指向臨時檔案
        RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
        //讀取臨時檔案的第一行資料
        randomFile.readLine();
        //讀取臨時檔案的第二行資料,這行資料中包含了檔案的路徑和檔案名稱
        String filePath = randomFile.readLine();
        System.out.println(filePath);
        //得到檔案名稱
        int position = filePath.lastIndexOf('\\');
        CodeToString codeToString = new CodeToString();
        String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
        //重新置放讀取檔案指標到檔案頭
        randomFile.seek(0);
        //得到第四行斷行符號符的位置,這是上傳檔案資料的開始位置
        long forthEnterPosition = 0;
        int forth = 1;
        while((n=randomFile.readByte())!=-1&&(forth<=4)){
         if(n=='\n'){
         forthEnterPosition = randomFile.getFilePointer();
         forth++;
         }
        }
        
            //產生上傳檔案的目錄
        File fileupLoad = new File("F:/MyEclipse/Manager/WebRoot/file","upLoad");
        fileupLoad.mkdir();
        //saveFile 對象指向要儲存的檔案
        File saveFile = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
        RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
        //找到上傳檔案資料的結束位置,即倒數第四行
        randomFile.seek(randomFile.length());
        long endPosition = randomFile.getFilePointer();
        int j = 1;
        while((endPosition>=0)&&(j<=4)){
         endPosition--;
         randomFile.seek(endPosition);
         if(randomFile.readByte()=='\n'){
         j++;
         }
        }
        
        //從上傳檔案資料的開始位置到結束位置,把資料寫入到要儲存的檔案中
        randomFile.seek(forthEnterPosition);
        long startPoint = randomFile.getFilePointer();
        while(startPoint<endPosition){
         randomAccessFile.write(randomFile.readByte());
         startPoint = randomFile.getFilePointer();
        }
        randomAccessFile.close();
        randomFile.close();
        tempFile.delete();
        
        TestExcel t=new TestExcel();
        t.add();
        
        
    }
 



} 真正的核心代碼,分析EXCEL

點擊(此處)摺疊或開啟 package control;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.