导入Excel核心代码
@ApiOperation("导入Excel")@PostMapping("/importExcel")public ActionResult importExcel(@RequestParam("file") MultipartFile file){if (file.getOriginalFilename().contains(".xlsx")) {ImportParams params = new ImportParams();params.setTitleRows(1);params.setHeadRows(1);List<ControlsEntity> list = null;try {list = ExcelImportUtil.importExcel(file.getInputStream(), ControlsEntity.class, params);} catch (Exception e) {e.printStackTrace();}//list.forEach(System.out::println);ActionResult actionResult = controlsService.importAddSave(list);return actionResult;} else {return ActionResult.fail("选择文件不符合导入");}// ActionResult actionResult = importExcelLocal();
// return actionResult;}
本地指定路径上传
/*** 本地测试* @return*/public ActionResult importExcelLocal(){String file = "D:\\workspace\\zsh_code\\i3_plat_portal\\temp\\1649669032468_import.xlsx";FileInputStream inputtSream = null;try {inputtSream = new FileInputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}ImportParams params = new ImportParams();params.setTitleRows(1);params.setHeadRows(1);List<ControlsEntity> list = null;try {list = ExcelImportUtil.importExcel(inputtSream, ControlsEntity.class, params);} catch (Exception e) {e.printStackTrace();}//list.forEach(System.out::println);ActionResult actionResult = controlsService.importAddSave(list);return actionResult;}
1,点击Body,
2,选择 form-data,
3,在KEY那栏,选择 File 类型
Key 对应接口参数名字 file
public ActionResult importExcel(@RequestParam("file") MultipartFile file){
4,Value处,浏览目录上传文件即可,如下图:会显示传递的参数名称 xxx.xlsx
这样就设置完成了,最后点击请求测试。
新增到数据库失败
新增到数据库成功