轉自: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.