Excel导入

  1. 首先要先拿到ExcelFile对象,然后使用ExcelUtils.formartExcelData 方法校验excel数据,最多支持26列数据。
  2. 支持数字,必填,邮箱,身份证验证,也支持自定义验证器,valid valueex.xxx的代表使用自定义校验器。
  1. List<MonthInsideCarTemplateDTO> insideCarTemplateDTOS = ExcelUtils.formartExcelData(MonthInsideCarTemplateDTO.class,file.getInputStream(),"[" +
  2. "{'index':'b','valid':['required'],'field':'ownerName'}," +
  3. "{'index':'c','field':'ownerMobile'}," +
  4. "{'index':'d','valid':['required','int'],'field':'lotNum'}," +
  5. "{'index':'e','valid':'ex.leaseType','field':'leaseTypeId'}," +
  6. "{'index':'f','valid':['required','double'],'field':'balance'}," +
  7. "{'index':'g','valid':'ex.plateNums','field':'plateNums'}," +
  8. "{'index':'h','valid':'ex.rule','field':'ruleId'}," +
  9. "{'index':'i','field':'startDate'}," +
  10. "{'index':'j','field':'endDate'}," +
  11. "]", 0,10,validator);

一个自定义验证器例子:

  1. private static class ImportDataValidator implements ExcelValidor {
  2. private Map<String,String> leaseTypeMap;
  3. private Map<String,String> monthlyRuleMap;
  4. private Set<String> plateNumberSet;
  5. private ParkLeaseTypeService parkLeaseTypeService; //租户类型Service
  6. private ParkMonthlyRuleService parkMonthlyRuleService; //月租收费规则
  7. private PayInsidePlateBindService payInsidePlateBindService;
  8. private String parkId;
  9. public ImportDataValidator(ParkMonthlyRuleService monthlyRuleService, ParkLeaseTypeService parkLeaseTypeService,String parkId,PayInsidePlateBindService payInsidePlateBindService){
  10. this.parkMonthlyRuleService = monthlyRuleService;
  11. this.parkLeaseTypeService=parkLeaseTypeService;
  12. this.parkId =parkId;
  13. this.payInsidePlateBindService = payInsidePlateBindService;
  14. }
  15. //(验证日期是yyyy-MM-dd支持闰年的正则表达式)
  16. // String rexp1 = "((\\d{2}(([02468][048])|([13579][26]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|(1[0-9])|(2[0-8]))))))";
  17. public boolean validParam(Object param, String valid, StringBuilder errorBuilder, char colName, int rowIndex) {
  18. if(CheckUtils.isNullOrEmpty(param))
  19. {
  20. errorBuilder.append("第" + rowIndex + "行,第" + colName + "列为必填;");
  21. return false;
  22. }
  23. boolean validedSuccess = false;
  24. switch (valid)
  25. {
  26. case "ex.rule":
  27. validedSuccess = monthlyRuleMap.containsKey(param);
  28. break;
  29. case "ex.leaseType":
  30. validedSuccess = leaseTypeMap.containsKey(param);
  31. break;
  32. case "ex.plateNums":
  33. String regets = ",|,|\\s+";
  34. String[] plateNums = ConverterUtils.toString(param).split(regets);
  35. boolean isAllPass = true;
  36. for(String palteNumber :plateNums)
  37. {
  38. if(plateNumberSet.contains(palteNumber))
  39. {
  40. isAllPass = false;
  41. continue;
  42. }
  43. plateNumberSet.add(ConverterUtils.toString(palteNumber));
  44. }
  45. validedSuccess = isAllPass;
  46. break;
  47. }
  48. if(!validedSuccess)
  49. {
  50. errorBuilder.append("第" + rowIndex + "行,第" + colName + "列输入有误;");
  51. return false;
  52. }
  53. return true;
  54. }
  55. public void init(Map<String, String> paramMap){
  56. List<ParkLeaseType> parkLeaseTypeList = parkLeaseTypeService.findForList(ParkLeaseType.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
  57. leaseTypeMap = parkLeaseTypeList.stream().collect(Collectors.toMap(ParkLeaseType::getLeaseName, ParkLeaseType::getId));
  58. List<ParkMonthlyRule> monthlyRuleList = parkMonthlyRuleService.findForList(ParkMonthlyRule.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
  59. monthlyRuleMap = monthlyRuleList.stream().collect(Collectors.toMap(ParkMonthlyRule::getRuleName, ParkMonthlyRule::getId));
  60. List<PayInsidePlateBind> insidePlateBindList = payInsidePlateBindService.findForList(PayInsidePlateBind.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
  61. plateNumberSet = insidePlateBindList.stream().map(PayInsidePlateBind::getPlateNumber).collect(Collectors.toSet());
  62. }
  63. }

如果校验失败,会抛出:IllegalArgumentException 获取 exception的message,即可给前台提示错误。