html:
<div>
<button type="button" class="layui-btn" id="uploadExcel">
<i class="layui-icon"></i>导入</button>
</div>
js:
<script>
layui.use('upload', function(){
var upload = layui.upload;
//执行实例
var uploadInst = upload.render({
elem: '#uploadExcel' //绑定元素
,url: '/evaluation/import' //上传接口
,accept:'file'
,field:"multipartFile"
,acceptMime:'file/xlsx,file/xls,file/xlsm,file/xltx'
,done: function(res){
if (res.code==200){
alert(res.msg)
layer.msg(res.msg)
setTimeout("alert(msg)",3000)
location.href='/evaluation/page';//返回列表页
}else{
layer.msg(res.msg);
}
}
});
});
</script>
java:
//导入
@RequestMapping("/evaluation/import")
@ResponseBody
public ResponseObject doImport(@RequestParam(value = "multipartFile", required = false) MultipartFile multipartFile, HttpServletResponse response) throws IOException {
int code=200;
msg="保存成功!";
String fileName = multipartFile.getOriginalFilename();
String xlsx= fileName.split("\\.")[1];
//xls返回的是HSSFWorkbook , 而xlsx返回的是XSSFWorkbook.
if("xlsx".equals(xlsx)){
XSSFWorkbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 1;i<=lastRowNum;i++){
try {
XSSFRow row = sheet.getRow(i);
SystemEvaluation bean=new SystemEvaluation();
bean.setId(UUID.randomUUID().toString());
bean.setEvaluation_mcss(row.getCell(0).toString());
bean.setEvaluation_tzqd(row.getCell(1).toString());
bean.setEvaluation_sjsywz(row.getCell(2).toString());
bean.setEvaluation_sjwzzzl(row.getCell(3).toString());
bean.setEvaluation_jckze(row.getCell(4).toString());
bean.setEvaluation_jkzzl(row.getCell(5).toString());
bean.setEvaluation_slbi(row.getCell(6).toString());
bean.setEvaluation_dnzjrs(row.getCell(7).toString());
bean.setEvaluation_rcgxd(row.getCell(8).toString());
bean.setEvaluation_cyjjd(row.getCell(9).toString());
bean.setEvaluation_sqtzbl(row.getCell(10).toString());
int s= service.insert(bean);
} catch (Exception e) {
e.printStackTrace();
msg="导入失败,检查导入格式!";
code=500;
}
}
}else if("xls".equals(xlsx)){
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(multipartFile.getInputStream());
HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
int lastNum = sheetAt.getLastRowNum();
for (int i = 1;i<=lastNum;i++) {
try {
HSSFRow row = sheetAt.getRow(i);
SystemEvaluation bean = new SystemEvaluation();
bean.setId(UUID.randomUUID().toString());
bean.setEvaluation_mcss(row.getCell(0).toString());
bean.setEvaluation_tzqd(row.getCell(1).toString());
bean.setEvaluation_sjsywz(row.getCell(2).toString());
bean.setEvaluation_sjwzzzl(row.getCell(3).toString());
bean.setEvaluation_jckze(row.getCell(4).toString());
bean.setEvaluation_jkzzl(row.getCell(5).toString());
bean.setEvaluation_slbi(row.getCell(6).toString());
bean.setEvaluation_dnzjrs(row.getCell(7).toString());
bean.setEvaluation_rcgxd(row.getCell(8).toString());
bean.setEvaluation_cyjjd(row.getCell(9).toString());
bean.setEvaluation_sqtzbl(row.getCell(10).toString());
int s = service.insert(bean);
} catch (Exception e) {
e.printStackTrace();
msg = "导入失败,检查导入格式!!";
code = 500;
}
}
}else {
msg = "导入失败,检查导入格式!!";
code = 500;
}
return new ResponseObject(code,msg);
}