操作excel貌似有兩個比較好的開源項目,這里用的是JAVA EXCEL API,參考文章如下
http://www./A200508/2005-08-10/190567.html
首先就下載所需的包了
http://www./jexcelapi/jexcelapi_2_6_6.tar.gz
解壓后,,只要里面的一個jxl.jar,,加入到工程的路徑中,。具體的操作和部分API參考文章
http://www-128.ibm.com/developerworks/cn/java/l-javaExcel/index.html
下面是從上傳導(dǎo)入的核心代碼,。
上傳頁面的jsp如下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
function myFormCheck(theform)
{
if(theform.theFile.value=="")
{
alert("請點(diǎn)擊瀏覽按鈕,,選擇您要上傳的文件!")
theform.theFile.focus;
return (false);
}
else
{
str= theform.theFile.value;
strs=str.toLowerCase();
lens=strs.length;
extname=strs.substring(lens-4,lens);
if(extname!=".xls")
{
alert("請選擇excel文件!")
return (false);
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<form name="uploadform" action="importdata.do" enctype="multipart/form-data" method=post onsubmit="return myFormCheck(this)">
<input type="file" name="theFile">
<input type="submit" value="導(dǎo)入">
</form>
</body>
</html>
其中用js從客戶端判斷了下文件的類型,。在服務(wù)端同樣判斷了類型,,這是防止某些人繞過js,直接上傳非excel文件,。
其中調(diào)用action importdata.do ,,