controller层

    1. @ApiOperation(value = "批量导入", notes = "导入模板,然后入库")
    2. @RequestMapping(value = "/list-import", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST)
    3. public RestResponse getExcels(@RequestParam("file") MultipartFile file) {
    4. try {
    5. String originalFileName = file.getOriginalFilename();
    6. String filename = originalFileName.substring(originalFileName.lastIndexOf("."));
    7. if (!".xls".equals(filename) && !".xlsx".equals(filename)) {
    8. return RestResponse.fail("请使用xlsx或xls文件进行导入");
    9. }
    10. RestResponse verifyResult = indicService.verifyList(file);
    11. return verifyResult;
    12. } catch (Exception var5) {
    13. log.error("模板导入失败", var5);
    14. return RestResponse.fail("模板数据不完整,请在模板内补全数据再进行导入");
    15. }
    16. }

    service层:

    1. @Override
    2. public RestResponse verifyList(MultipartFile file) throws Exception {
    3. String originalName = file.getOriginalFilename();
    4. String name = file.getName();
    5. logger.info("=导入==开始==originalName:" + originalName);
    6. logger.info("=导入==开始==name:" + name);
    7. ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
    8. Set<String> listTemp = new HashSet<String>();
    9. List<List<Object>> impDataList = excelReader.read(0);
    10. String message = "";
    11. String lineString = "";
    12. boolean flag = false;
    13. if (CollUtil.isNotEmpty(impDataList)) {
    14. String titleStr = ((List) impDataList.get(0)).get(0).toString();
    15. logger.info("=导入====titleStr:" + titleStr);
    16. if (!titleStr.contains("编码")) {
    17. throw new BusinessException("请使用正确的指标导入模板导入数据");
    18. } else {
    19. List<List<Object>> impData = excelReader.read(1);
    20. if (ObjectUtil.isNotNull(impData)) {
    21. List<dao1> indicList = new ArrayList<>();
    22. boolean rowBoolean = true;
    23. for (List<Object> row : impData) {
    24. dao1 entity = new dao1();
    25. if (StrUtil.isNotEmpty(Convert.toStr(row.get(0)))) {
    26. entity.setIndicKey(Convert.toStr(row.get(0)));//指标编码
    27. }else{
    28. rowBoolean = false;
    29. break;
    30. }
    31. if (StrUtil.isNotEmpty(Convert.toStr(row.get(1)))) {
    32. entity.setIndicName(Convert.toStr(row.get(1)));//指标名称
    33. }else{
    34. message = "指标名称为空!请修改后重试";
    35. rowBoolean = false;
    36. break;
    37. }
    38. //指标描述 ---- 可以为空
    39. entity.setIndicDescription(Convert.toStr(row.get(2)));
    40. indicList.add(entity);
    41. }
    42. if(rowBoolean == true && CollectionUtils.isNotEmpty(indicList)){
    43. flag = this.addIndicList(indicList);//批量导入
    44. }else{
    45. return RestResponse.fail(message);
    46. }
    47. if (flag) {
    48. message = "成功导入" + (impDataList.size() - 1) + "条数据";
    49. if (impDataList.size() - 1 == 0) {
    50. return RestResponse.fail("模板数据为空,请在模板内添加数据再进行导入");
    51. }
    52. } else {
    53. return RestResponse.fail("请使用正确的指标导入模板导入数据");
    54. }
    55. } else {
    56. return RestResponse.fail("请使用正确的指标导入模板导入数据");
    57. }
    58. }
    59. }
    60. return RestResponse.successMsg(message);
    61. }