今天说下excel数据导入数据库,我使用的是jxl,首先项目中应该有jxl的jar
下面说说页面,选择xls文件
<form name="form1" action ="ExcelAction" method="post">
<input name="file" type ="file" />
<input type ="submit" value = "import">
</form>
action 配置这里就不说了,
具体说说action 如何操作
String path = request.getParameter("file");
InputStream is = new FileInputStream(path);
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0); //读取在职sheet
String message0 = excelManage.ExcelEmployee(rs);
excelManage这个是我的spring 中的bean ,你们也可以直接进行导入操作。
具体实现:
String message = "";
int colNum=rs.getColumns();//列数
int rowNum=rs.getRows();//行数
System.out.println("rowNum colNum ------------------"+rowNum+","+colNum);
for(int j=1;j<rowNum;j++){
TEmployee tEmployee = new TEmployee();
for(int i=0;i<colNum;i++){
Cell c = rs.getCell(i,j);
String strc = c.getContents();
System.out.println("Cell("+i+", "+j+")" + " value : " + strc + "; type : " + c.getType());
}
}
c.getContents();则为某行列的值,可以将此值放入所插入对象中,最后save(Entityobject)
这篇文章可借鉴,有一定共性。