目录
1、控制层
因为前端设置了只能上传1个文件,这里直接取一个。
@RequestMapping(value = "/shebeiDaoru.ctrl", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public @ResponseBody Map<String, Object> shebeiDaoru(HttpServletRequest request,
@RequestParam("file") MultipartFile[] files) {
return shebeiService.shebeiDaoru(files[0]);
}
2、业务层(主要逻辑)
通过multipartFile直接获取输入流,构建HSSFWorkbook。
@SuppressWarnings("resource")
@Override
public Map<String, Object> shebeiDaoru(MultipartFile multipartFile) {
try {
Workbook workbook = new HSSFWorkbook(multipartFile.getInputStream());
// 获取第一张sheet。
Sheet sheetAt = workbook.getSheetAt(0);
int index=0;
for(Row row:sheetAt) {
//跳过第一行
if(index==0) {
index++;
continue;
}
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
Shebei shebei = new Shebei();
shebei.setCode(cell0.getStringCellValue());
shebei.setName(cell1.getStringCellValue());
shebei.setStatus(JConstant.status_1);
shebeiMapper.insert(shebei);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
参考文献: