jsp struts2导入excel并且存储到数据库中

开发中遇到一个问题:

需要从外部导入excel,拿到其中的数据然后保存到数据库中。

1、先在jsp端使用input进行上传:

<form action="storeOBDexcel" method="post"
enctype="multipart/form-data">
<input type="file" name="excelfile" onchange="checkfile(this)">
<input type="submit" value="导入">
</form>

2、在Action中进行接收:

private File excelfile;

{getter,setter省略}

3、读取文件(struts暂时保存在临时文件中,可直接使用)

需要引入jxl.jar包,下载地址:“http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/”

ReadExcel(this.excelfile);

public ArrayList<OBDBean> ReadExcel(File file){
ArrayList<OBDBean> list=new ArrayList<OBDBean>();
try {
Workbook wb=Workbook.getWorkbook(file);
Sheet sheet=wb.getSheet(0);
for(int i=1;i<sheet.getRows();i++){
for(int j=0;j<sheet.getColumns();j++){
Cell cell=sheet.getCell(j, i);
system.out.println(cell.getContents());
}
}
return list;

}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

成功获取并使用

原文地址:https://www.cnblogs.com/ichemmwangd/p/4616332.html