操作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
下面是从上传导入的核心代码。
上传页面的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("请点击浏览按钮,选择您要上传的文件!")
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="导入">
</form>
</body>
</html>
其中用js从客户端判断了下文件的类型。在服务端同样判断了类型,这是防止某些人绕过js,直接上传非excel文件。
其中调用action importdata.do ,