若依框架实现 导入excel时校验必填字段
@Excel(name = "姓名",required = true) private String personName;
3.在ExcelUtil类的importExcel方法里,获取是否必填属性并进行校验
if (StringUtils.isNotNull(fieldType)) { String propertyName = field.getName(); if (StringUtils.isNotEmpty(attr.targetAttr())) { propertyName = field.getName() + "." + attr.targetAttr(); } //获取是否有必填属性 boolean required = attr.required(); if(required) { if(StringUtils.isNull(val) || StringUtils.isEmpty(val.toString())) { throw new Exception(attr.name()+" 单元格未填写,请正确填写!"); } } if (StringUtils.isNotEmpty(attr.readConverterExp())) { val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator()); } else if (StringUtils.isNotEmpty(attr.dictType())) { val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator()); } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { val = dataFormatHandlerAdapter(val, attr, null); } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) { PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey()); if (image == null) { val = ""; } else { byte[] data = image.getData(); val = FileUtils.writeImportBytes(data); } } ReflectUtils.invokeSetter(entity, propertyName, val); } }
4.页面效果
本文原创,转载必追究版权。