当前位置:Gxlcms > 数据库问题 > jsp struts2导入excel并且存储到数据库中

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

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;
}

成功获取并使用

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

标签:

人气教程排行