Excel导入
首先要先拿到Excel的File对象,然后使用ExcelUtils.formartExcelData 方法校验excel数据,最多支持26列数据。
支持数字,必填,邮箱,身份证验证,也支持自定义验证器,valid value是ex.xxx的代表使用自定义校验器。
List<MonthInsideCarTemplateDTO> insideCarTemplateDTOS = ExcelUtils.formartExcelData(MonthInsideCarTemplateDTO.class,file.getInputStream(),"[" +
"{'index':'b','valid':['required'],'field':'ownerName'}," +
"{'index':'c','field':'ownerMobile'}," +
"{'index':'d','valid':['required','int'],'field':'lotNum'}," +
"{'index':'e','valid':'ex.leaseType','field':'leaseTypeId'}," +
"{'index':'f','valid':['required','double'],'field':'balance'}," +
"{'index':'g','valid':'ex.plateNums','field':'plateNums'}," +
"{'index':'h','valid':'ex.rule','field':'ruleId'}," +
"{'index':'i','field':'startDate'}," +
"{'index':'j','field':'endDate'}," +
"]", 0,10,validator);
一个自定义验证器例子:
private static class ImportDataValidator implements ExcelValidor {
private Map<String,String> leaseTypeMap;
private Map<String,String> monthlyRuleMap;
private Set<String> plateNumberSet;
private ParkLeaseTypeService parkLeaseTypeService; //租户类型Service
private ParkMonthlyRuleService parkMonthlyRuleService; //月租收费规则
private PayInsidePlateBindService payInsidePlateBindService;
private String parkId;
public ImportDataValidator(ParkMonthlyRuleService monthlyRuleService, ParkLeaseTypeService parkLeaseTypeService,String parkId,PayInsidePlateBindService payInsidePlateBindService){
this.parkMonthlyRuleService = monthlyRuleService;
this.parkLeaseTypeService=parkLeaseTypeService;
this.parkId =parkId;
this.payInsidePlateBindService = payInsidePlateBindService;
}
//(验证日期是yyyy-MM-dd支持闰年的正则表达式)
// 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]))))))";
public boolean validParam(Object param, String valid, StringBuilder errorBuilder, char colName, int rowIndex) {
if(CheckUtils.isNullOrEmpty(param))
{
errorBuilder.append("第" + rowIndex + "行,第" + colName + "列为必填;");
return false;
}
boolean validedSuccess = false;
switch (valid)
{
case "ex.rule":
validedSuccess = monthlyRuleMap.containsKey(param);
break;
case "ex.leaseType":
validedSuccess = leaseTypeMap.containsKey(param);
break;
case "ex.plateNums":
String regets = ",|,|\\s+";
String[] plateNums = ConverterUtils.toString(param).split(regets);
boolean isAllPass = true;
for(String palteNumber :plateNums)
{
if(plateNumberSet.contains(palteNumber))
{
isAllPass = false;
continue;
}
plateNumberSet.add(ConverterUtils.toString(palteNumber));
}
validedSuccess = isAllPass;
break;
}
if(!validedSuccess)
{
errorBuilder.append("第" + rowIndex + "行,第" + colName + "列输入有误;");
return false;
}
return true;
}
public void init(Map<String, String> paramMap){
List<ParkLeaseType> parkLeaseTypeList = parkLeaseTypeService.findForList(ParkLeaseType.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
leaseTypeMap = parkLeaseTypeList.stream().collect(Collectors.toMap(ParkLeaseType::getLeaseName, ParkLeaseType::getId));
List<ParkMonthlyRule> monthlyRuleList = parkMonthlyRuleService.findForList(ParkMonthlyRule.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
monthlyRuleMap = monthlyRuleList.stream().collect(Collectors.toMap(ParkMonthlyRule::getRuleName, ParkMonthlyRule::getId));
List<PayInsidePlateBind> insidePlateBindList = payInsidePlateBindService.findForList(PayInsidePlateBind.builder().parkId(paramMap.get("parkId")).groupCode(paramMap.get("groupCode")).build());
plateNumberSet = insidePlateBindList.stream().map(PayInsidePlateBind::getPlateNumber).collect(Collectors.toSet());
}
}
如果校验失败,会抛出:IllegalArgumentException 获取 exception的message,即可给前台提示错误。
当前内容版权归 fhs-opensource 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 fhs-opensource .