这是一个在jsp文件中实现的excel导入数据库的java代码有什么不足之处还请谅解并告知我kyxiaomei@126.com
注明:红色字体的为关键的java代码
//首先导入要set的实体对象
<%@ page import="cheliang.Cheliang_zhejiu,cheliang.DeviceList" %>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../../images/all.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/bpm/resources/libs/dojo/v1.2.0/dojo/dojo.js"></script>
</head>
<%
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
su.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
//su.save("/portal/upload/files");
// 利用Request对象获取参数之值
// 逐一提取上传文件信息,同时可保存文件。
com.jspsmart.upload.File file = su.getFiles().getFile(0);
file.saveAs("/portal/upload/files/" + "newAddCurrentSum."+file.getFileExt(),su.SAVE_VIRTUAL);
if (file.isMissing()){
return;
}
String resourcePath=request.getRealPath("/portal/upload/files/");
FileInputStream readFile = new FileInputStream(resourcePath.replace("/","//")+"//newAddCurrentSum.xls");
// 创建一个WorkBook,从指定的文件流中创建,即上面指定了的文件流
HSSFWorkbook wb = new HSSFWorkbook(readFile);
//获得workbook中sheet的个数
int sheetnumber=wb.getNumberOfSheets();
HSSFRow row=null;
HSSFCell cell=null;
EntityManager jpa=null;
//遍历workbook中说有的sheet
try{
jpa=DynaEntityManagerFactory.getInstance().createEntityManager();
jpa.joinTransaction();
//循环所有的sheet
for(int i=0;i<sheetnumber;i++){
HSSFSheet sheet = wb.getSheetAt(i);
//获得当前sheet中所有的行
int rows = sheet.getPhysicalNumberOfRows();
for (int r=0; r<rows;r++) {
row=sheet.getRow(r);
if (row==null){
continue;
}
//获得当前sheet的第一列单元格,从0开始
cell=row.getCell((short)0);
//创建要导入的实体对象
Model zj=new Model();
zj.setDeviceID(device.getDeviceId());
//要导入的单元格起始位置
cell=row.getCell((short)6);
String leijizhejiu=null;
//通常单元格的值有数字形式,字符串形式,公式形式,下标形式
//如果单元格的值的类型==0时返回int public static final int CELL_TYPE_NUMERIC = 0;
if(cell.getCellType()==0){
leijizhejiu=cell.getNumericCellValue()+"";
zj.setLeijiZhejiu(new BigDecimal(leijizhejiu));
}
//如果单元格的值的类型==1时返回int public static final int CELL_TYPE_STRING = 1;
if(cell.getCellType()==1){
leijizhejiu=cell.getStringCellValue();
zj.setLeijiZhejiu(new BigDecimal(leijizhejiu));
}
//如果单元格的值的类型getCellType()==2时返回int public static final int CELL_TYPE_STRING = 2;
if(cell.getCellType()==2){
leijizhejiu=cell.getNumericCellValue()+"";
zj.setLeijiZhejiu(new BigDecimal(leijizhejiu));
}
//如果单元格的值的类型getCellType()==3时返回int public static final int CELL_TYPE_BLANK = 3;
if(cell.getCellType()==3){
leijizhejiu=cell.getNumericCellValue()+"";
zj.setLeijiZhejiu(new BigDecimal(leijizhejiu));
}
}
//执行插入操作
jpa.persist(zj);
}
}
}
jpa.getTransaction().commit();
}catch(Exception e){
jpa.getTransaction().rollback();
e.printStackTrace();
}finally{
if(jpa!=null){
jpa.close();
}
}
%>
</script>
<body ></body>
</html>