diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java index 0cb50d59..2d2eaac7 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java @@ -73,7 +73,7 @@ public class DeptAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(Dept.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictAjax.java index 9c319916..99de7b1a 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictAjax.java @@ -73,7 +73,7 @@ public class DictAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(Dict.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/FileAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/FileAjax.java index f235e06c..3f559484 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/FileAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/FileAjax.java @@ -67,7 +67,7 @@ public class FileAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(File.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/ResAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/ResAjax.java index f52de735..7c3ef991 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/ResAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/ResAjax.java @@ -73,7 +73,7 @@ public class ResAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(Res.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/RoleAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/RoleAjax.java index 976731e6..92aa3d25 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/RoleAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/RoleAjax.java @@ -67,7 +67,7 @@ public class RoleAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(Role.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/TokensAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/TokensAjax.java index fd834335..7330ca2e 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/TokensAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/TokensAjax.java @@ -73,7 +73,7 @@ public class TokensAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(Tokens.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java index 6f0a8d27..a96f147a 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java @@ -208,7 +208,7 @@ public class UserAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WExcel<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(User.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { diff --git a/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java b/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java index b5d4fe38..aa825301 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java +++ b/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.excel; +package ${basePackage}.frame.excel; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -13,34 +13,35 @@ import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import xyz.wbsite.frame.excel.annotation.ColumnDescription; -import xyz.wbsite.frame.excel.annotation.ColumnName; -import xyz.wbsite.frame.excel.annotation.Ignore; -import xyz.wbsite.frame.excel.annotation.ParentFirst; -import xyz.wbsite.frame.excel.annotation.SheetName; -import xyz.wbsite.frame.excel.converter.BooleanConverter; -import xyz.wbsite.frame.excel.converter.ByteConverter; -import xyz.wbsite.frame.excel.converter.CharacterConverter; -import xyz.wbsite.frame.excel.converter.Converter; -import xyz.wbsite.frame.excel.converter.DateConverter; -import xyz.wbsite.frame.excel.converter.DoubleConverter; -import xyz.wbsite.frame.excel.converter.FloatConverter; -import xyz.wbsite.frame.excel.converter.IntegerConverter; -import xyz.wbsite.frame.excel.converter.LongConverter; -import xyz.wbsite.frame.excel.converter.ShortConverter; -import xyz.wbsite.frame.excel.converter.StringConverter; -import xyz.wbsite.frame.excel.exception.ReadErrorException; -import xyz.wbsite.frame.excel.exception.TemplateNotMatchException; -import xyz.wbsite.frame.excel.exception.ValueConverterException; -import xyz.wbsite.frame.excel.style.DataCellStyle; -import xyz.wbsite.frame.excel.style.ErrorCellStyle; -import xyz.wbsite.frame.excel.style.HeadCellStyle; -import xyz.wbsite.frame.excel.style.RedFont; -import xyz.wbsite.frame.utils.ClassUtil; -import xyz.wbsite.frame.utils.FileUtil; -import xyz.wbsite.frame.utils.LogUtil; -import xyz.wbsite.frame.utils.ValidationUtil; -import xyz.wbsite.module.system.ent.Dept; +import ${basePackage}.frame.excel.annotation.ColumnDescription; +import ${basePackage}.frame.excel.annotation.ColumnName; +import ${basePackage}.frame.excel.annotation.Ignore; +import ${basePackage}.frame.excel.annotation.ParentFirst; +import ${basePackage}.frame.excel.annotation.SheetName; +import ${basePackage}.frame.excel.converter.BooleanConverter; +import ${basePackage}.frame.excel.converter.ByteConverter; +import ${basePackage}.frame.excel.converter.CharacterConverter; +import ${basePackage}.frame.excel.converter.Converter; +import ${basePackage}.frame.excel.converter.DateConverter; +import ${basePackage}.frame.excel.converter.DoubleConverter; +import ${basePackage}.frame.excel.converter.FloatConverter; +import ${basePackage}.frame.excel.converter.IntegerConverter; +import ${basePackage}.frame.excel.converter.LongConverter; +import ${basePackage}.frame.excel.converter.ShortConverter; +import ${basePackage}.frame.excel.converter.StringConverter; +import ${basePackage}.frame.excel.exception.ReadErrorException; +import ${basePackage}.frame.excel.exception.TemplateNotMatchException; +import ${basePackage}.frame.excel.exception.ValueConverterException; +import ${basePackage}.frame.excel.style.DataCellStyle; +import ${basePackage}.frame.excel.style.ErrorCellStyle; +import ${basePackage}.frame.excel.style.HeadCellStyle; +import ${basePackage}.frame.excel.style.RedFont; +import ${basePackage}.frame.excel.style.SuccessCellStyle; +import ${basePackage}.frame.utils.ClassUtil; +import ${basePackage}.frame.utils.FileUtil; +import ${basePackage}.frame.utils.LogUtil; +import ${basePackage}.frame.utils.ValidationUtil; +import ${basePackage}.module.system.ent.Dept; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -54,7 +55,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; @@ -147,8 +147,8 @@ public class WExcel implements Serializable, Cloneable { } //获取列类型 - if (field.isAnnotationPresent(xyz.wbsite.frame.excel.annotation.Converter.class)) { - xyz.wbsite.frame.excel.annotation.Converter converter = field.getAnnotation(xyz.wbsite.frame.excel.annotation.Converter.class); + if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.Converter.class)) { + ${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class); Class target = converter.target(); try { WColumn.setConverter((Converter) target.newInstance()); @@ -204,7 +204,7 @@ public class WExcel implements Serializable, Cloneable { * * @param list 需要导出的对象列表 */ - public xyz.wbsite.frame.excel.WExcel loadData(List list) { + public WExcel loadData(List list, Processor processor) { if (list.size() > 0) { for (T t : list) { WRow row = new WRow(); @@ -226,6 +226,12 @@ public class WExcel implements Serializable, Cloneable { } catch (InvocationTargetException e) { LogUtil.w(method.getName() + " unexpected exception!"); } + // <2层检查>检查模板注解验证是否通过 + row.addErrors(ValidationUtil.validate(t)); + // <3层检查>如果没有错误,则可以执行处理器进行逻辑处理 + if (!row.hasError() && processor != null) { + row.addErrors(processor.exec(t)); + } } this.rowList.add(row); } @@ -233,15 +239,15 @@ public class WExcel implements Serializable, Cloneable { return this; } - public xyz.wbsite.frame.excel.WExcel check(byte[] bytes) throws TemplateNotMatchException, ReadErrorException { - return check(bytes, null); + public WExcel loadData(byte[] bytes) throws TemplateNotMatchException, ReadErrorException { + return loadData(bytes, null); } /** * 以Excel文件的字节数组为数据源,为自己赋值的同时,与指定的模板类进行数据校验。
* 如果匹配过程中发现了不符合的数据,会在对应的单元中添加错误信息。
*/ - public xyz.wbsite.frame.excel.WExcel check(byte[] bytes, Validator validator) throws TemplateNotMatchException, ReadErrorException { + public WExcel loadData(byte[] bytes, Processor processor) throws TemplateNotMatchException, ReadErrorException { Workbook workbook = null; InputStream is = null; boolean flag; @@ -331,13 +337,13 @@ public class WExcel implements Serializable, Cloneable { try { T t = templateClass.newInstance(); - List errs = transferMap(row, t); - errs.addAll(ValidationUtil.validate(t)); - errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList()); - if (errs.size() > 0) { - for (String s : errs) { - row.addError(s); - } + // <1层检查>检查数据格式是否正确 + row.addErrors(transferMap(row, t)); + // <2层检查>检查模板注解验证是否通过 + row.addErrors(ValidationUtil.validate(t)); + // <3层检查>如果没有错误,则可以执行处理器进行逻辑处理 + if (!row.hasError() && processor != null) { + row.addErrors(processor.exec(t)); } } catch (InstantiationException | IllegalAccessException e) { row.addError("模板对象默认构造函数错误"); @@ -545,11 +551,11 @@ public class WExcel implements Serializable, Cloneable { } else { // 添加结果 Cell resultCell = row.createCell(0); - resultCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle()); + resultCell.setCellStyle(new SuccessCellStyle(workbook).getStyle()); resultCell.setCellValue("成功"); // 添加错误信息详细说明 Cell errsCell = row.createCell(1); - errsCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle()); + errsCell.setCellStyle(new SuccessCellStyle(workbook).getStyle()); errsCell.setCellValue(""); } @@ -604,8 +610,8 @@ public class WExcel implements Serializable, Cloneable { } } - public interface Validator { - List validate(T t); + public interface Processor { + List exec(T t); } public void toFile(File file) throws IOException { @@ -634,7 +640,7 @@ public class WExcel implements Serializable, Cloneable { // 检查excel文件是否符合导入 byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx")); try { - xyz.wbsite.frame.excel.WExcel check = new xyz.wbsite.frame.excel.WExcel(Dept.class).check(bytes); + WExcel check = new WExcel(Dept.class).loadData(bytes); check.toFile(new File("E:\\E_err.xlsx")); System.out.println(check.hasError()); } catch (TemplateNotMatchException e) { diff --git a/src/main/resources/modules/SpringBoot/java/frame/excel/WRow.java b/src/main/resources/modules/SpringBoot/java/frame/excel/WRow.java index 4cd4638a..a63ae4d3 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/excel/WRow.java +++ b/src/main/resources/modules/SpringBoot/java/frame/excel/WRow.java @@ -24,6 +24,10 @@ public class WRow extends HashMap implements Serializable { errorList.add(errorMsg); } + public final void addErrors(List errorMsgs) { + errorList.addAll(errorMsgs); + } + public List getErrorList() { return errorList; }