From 042aa18a456c2d83fb8cc234b49b6b77e531a6dc Mon Sep 17 00:00:00 2001 From: wangbing Date: Sun, 19 Jan 2020 17:36:28 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81Excel=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 9db9fc9480eb5a56f35bb196f2ef3d50d7825a7f --- .../SpringBoot/java/action/ajax/Ajax.java | 8 +- .../java/action/ajax/system/DeptAjax.java | 8 +- .../java/action/ajax/system/DictAjax.java | 8 +- .../java/action/ajax/system/DictItemAjax.java | 8 +- .../java/action/ajax/system/FileAjax.java | 8 +- .../java/action/ajax/system/ResAjax.java | 8 +- .../java/action/ajax/system/RoleAjax.java | 8 +- .../java/action/ajax/system/TokensAjax.java | 8 +- .../java/action/ajax/system/UserAjax.java | 8 +- .../SpringBoot/java/frame/excel/WCell.java | 14 +- .../frame/excel/{WSheet.java => WExcel.java} | 282 +++++++++++------- .../frame/excel/style/SuccessCellStyle.java | 22 ++ .../java/frame/utils/ResponseUtil.java | 4 +- .../module/system/mgr/TokensManagerImpl.java | 2 +- 14 files changed, 255 insertions(+), 141 deletions(-) rename src/main/resources/modules/SpringBoot/java/frame/excel/{WSheet.java => WExcel.java} (67%) create mode 100644 src/main/resources/modules/SpringBoot/java/frame/excel/style/SuccessCellStyle.java diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/Ajax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/Ajax.java index 65774090..cd5c1737 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/Ajax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/Ajax.java @@ -3,7 +3,7 @@ package ${basePackage}.action.ajax.${module}; import com.fasterxml.jackson.core.TreeNode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.utils.LogUtil; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.ErrorType; @@ -64,7 +64,7 @@ public class ${table.getCName()}Ajax{ <#if table.getHtml()> public Object template(){ - return ResponseUtil.apply(new WSheet<>(${table.getCName()}.class)); + return ResponseUtil.apply(new WExcel<>(${table.getCName()}.class)); } public Object exports(TreeNode jsonParam) { @@ -76,13 +76,13 @@ public class ${table.getCName()}Ajax{ response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet<${table.getCName()}> sheet = new WSheet<>(file.getBytes(), ${table.getCName()}.class, new WSheet.Validator<${table.getCName()}>() { + WExcel<${table.getCName()}> sheet = new WExcel<>(file.getBytes(), ${table.getCName()}.class, new WExcel.Validator<${table.getCName()}>() { @Override public List validate(${table.getCName()} o) { ${table.getCName()}CreateRequest request = MapperUtil.map(o, ${table.getCName()}CreateRequest.class); 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 4c8e41ab..0cb50d59 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 @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -61,7 +61,7 @@ public class DeptAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(Dept.class)); + return ResponseUtil.apply(new WExcel<>(Dept.class)); } public Object exports(TreeNode jsonParam) { @@ -73,13 +73,13 @@ public class DeptAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), Dept.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), Dept.class, new WExcel.Validator() { @Override public List validate(Dept o) { DeptCreateRequest request = MapperUtil.map(o, DeptCreateRequest.class); 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 a052a2f9..9c319916 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 @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -61,7 +61,7 @@ public class DictAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(Dict.class)); + return ResponseUtil.apply(new WExcel<>(Dict.class)); } public Object exports(TreeNode jsonParam) { @@ -73,13 +73,13 @@ public class DictAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), Dict.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), Dict.class, new WExcel.Validator() { @Override public List validate(Dict o) { DictCreateRequest request = MapperUtil.map(o, DictCreateRequest.class); diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictItemAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictItemAjax.java index eedd2b80..f9c7b793 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictItemAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DictItemAjax.java @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -55,7 +55,7 @@ public class DictItemAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(DictItem.class)); + return ResponseUtil.apply(new WExcel<>(DictItem.class)); } public Object exports(TreeNode jsonParam) { @@ -67,13 +67,13 @@ public class DictItemAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel(DictItem.class).loadData(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), DictItem.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), DictItem.class, new WExcel.Validator() { @Override public List validate(DictItem o) { DictItemCreateRequest request = MapperUtil.map(o, DictItemCreateRequest.class); 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 57b1304a..f235e06c 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 @@ -10,7 +10,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -55,7 +55,7 @@ public class FileAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(File.class)); + return ResponseUtil.apply(new WExcel<>(File.class)); } public Object exports(TreeNode jsonParam) { @@ -67,13 +67,13 @@ public class FileAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), File.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), File.class, new WExcel.Validator() { @Override public List validate(File o) { FileCreateRequest request = MapperUtil.map(o, FileCreateRequest.class); 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 07582a45..f52de735 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 @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -61,7 +61,7 @@ public class ResAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(Res.class)); + return ResponseUtil.apply(new WExcel<>(Res.class)); } public Object exports(TreeNode jsonParam) { @@ -73,13 +73,13 @@ public class ResAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), Res.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), Res.class, new WExcel.Validator() { @Override public List validate(Res o) { ResCreateRequest request = MapperUtil.map(o, ResCreateRequest.class); 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 ca950e70..976731e6 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 @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -55,7 +55,7 @@ public class RoleAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(Role.class)); + return ResponseUtil.apply(new WExcel<>(Role.class)); } public Object exports(TreeNode jsonParam) { @@ -67,13 +67,13 @@ public class RoleAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), Role.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), Role.class, new WExcel.Validator() { @Override public List validate(Role o) { RoleCreateRequest request = MapperUtil.map(o, RoleCreateRequest.class); 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 bf38f996..fd834335 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 @@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.LogUtil; @@ -61,7 +61,7 @@ public class TokensAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(Tokens.class)); + return ResponseUtil.apply(new WExcel<>(Tokens.class)); } public Object exports(TreeNode jsonParam) { @@ -73,13 +73,13 @@ public class TokensAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), Tokens.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), Tokens.class, new WExcel.Validator() { @Override public List validate(Tokens o) { TokensCreateRequest request = MapperUtil.map(o, TokensCreateRequest.class); 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 2d2b64b8..6f0a8d27 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 @@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import ${basePackage}.frame.excel.exception.ReadErrorException; import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.frame.utils.CookieUtil; @@ -196,7 +196,7 @@ public class UserAjax { } public Object template() { - return ResponseUtil.apply(new WSheet<>(User.class)); + return ResponseUtil.apply(new WExcel<>(User.class)); } public Object exports(TreeNode jsonParam) { @@ -208,13 +208,13 @@ public class UserAjax { response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); return response; } - return ResponseUtil.apply(new WSheet<>(response.getResult())); + return ResponseUtil.apply(new WExcel<>(response.getResult())); } public Object imports(MultipartFile file) { BaseResponse baseResponse = new BaseResponse(); try { - WSheet sheet = new WSheet<>(file.getBytes(), User.class, new WSheet.Validator() { + WExcel sheet = new WExcel<>(file.getBytes(), User.class, new WExcel.Validator() { @Override public List validate(User o) { UserCreateRequest request = MapperUtil.map(o, UserCreateRequest.class); diff --git a/src/main/resources/modules/SpringBoot/java/frame/excel/WCell.java b/src/main/resources/modules/SpringBoot/java/frame/excel/WCell.java index 44f24938..d42bfacf 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/excel/WCell.java +++ b/src/main/resources/modules/SpringBoot/java/frame/excel/WCell.java @@ -1,6 +1,5 @@ package ${basePackage}.frame.excel; - import java.io.Serializable; /** @@ -17,6 +16,11 @@ public class WCell implements Serializable { */ private String value; + /** + * 单元格存在的错误,只在excel导入时会产生错误 + */ + private String error; + public WCell() { this.value = ""; } @@ -32,4 +36,12 @@ public class WCell implements Serializable { public void setValue(String value) { this.value = value; } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } } diff --git a/src/main/resources/modules/SpringBoot/java/frame/excel/WSheet.java b/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java similarity index 67% rename from src/main/resources/modules/SpringBoot/java/frame/excel/WSheet.java rename to src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java index 2bf46776..b5d4fe38 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/excel/WSheet.java +++ b/src/main/resources/modules/SpringBoot/java/frame/excel/WExcel.java @@ -1,39 +1,71 @@ -package ${basePackage}.frame.excel; - -import ${basePackage}.frame.excel.converter.Converter; -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.utils.ClassUtil; -import ${basePackage}.frame.utils.LogUtil; -import ${basePackage}.frame.utils.ValidationUtil; -import ${basePackage}.frame.excel.annotation.*; -import ${basePackage}.frame.excel.converter.*; -import ${basePackage}.frame.excel.exception.ReadErrorException; -import ${basePackage}.frame.excel.exception.TemplateNotMatchException; +package xyz.wbsite.frame.excel; + import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.*; - -import javax.validation.constraints.NotNull; -import java.io.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; +import org.apache.poi.xssf.usermodel.XSSFComment; +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 java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; /** - * WSheet - Excel-Sheet对象,完整的数据集合对象。 + * WExcel - Excel-Sheet对象,完整的数据集合对象。 * * @author wangbing * @version 0.0.1 * @since 2017-01-01 */ -public class WSheet implements Serializable, Cloneable { +public class WExcel implements Serializable, Cloneable { /** * 表名 @@ -41,10 +73,15 @@ public class WSheet implements Serializable, Cloneable { private String name; /** - * 表名 + * 是否冻结第一行 */ private boolean freezeFirst; + /** + * 模板类 + */ + private Class templateClass; + /** * 表头的集合 */ @@ -58,7 +95,8 @@ public class WSheet implements Serializable, Cloneable { /** * 根据输入的模板类打印下载模板。 */ - public WSheet(Class tClass) { + public WExcel(Class tClass) { + this.templateClass = tClass; initColumns(tClass); } @@ -109,8 +147,8 @@ public class WSheet implements Serializable, Cloneable { } //获取列类型 - if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.Converter.class)) { - ${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class); + 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); Class target = converter.target(); try { WColumn.setConverter((Converter) target.newInstance()); @@ -166,16 +204,11 @@ public class WSheet implements Serializable, Cloneable { * * @param list 需要导出的对象列表 */ - public WSheet(List list) { - if (list == null || list.size() == 0) { - throw new RuntimeException("导出数据为空"); - } + public xyz.wbsite.frame.excel.WExcel loadData(List list) { if (list.size() > 0) { - List wColumns = initColumns(list.get(0).getClass()); - for (T t : list) { WRow row = new WRow(); - for (WColumn column : wColumns) { + for (WColumn column : columnList) { if (column == null) { continue; } @@ -197,22 +230,18 @@ public class WSheet implements Serializable, Cloneable { this.rowList.add(row); } } + return this; + } + + public xyz.wbsite.frame.excel.WExcel check(byte[] bytes) throws TemplateNotMatchException, ReadErrorException { + return check(bytes, null); } /** * 以Excel文件的字节数组为数据源,为自己赋值的同时,与指定的模板类进行数据校验。
* 如果匹配过程中发现了不符合的数据,会在对应的单元中添加错误信息。
- * - * @param bytes Excel文件的字节数组 - * @param clazz 类型 - * @throws IOException
1:输入流无法转为Excel文件 - 请检查输入的 bytes 是否正确;
- * 2:输入流关闭出错;
*/ - public WSheet(byte[] bytes, Class clazz) throws TemplateNotMatchException, ReadErrorException { - this(bytes, clazz, null); - } - - public WSheet(byte[] bytes, Class clazz, Validator validator) throws TemplateNotMatchException, ReadErrorException { + public xyz.wbsite.frame.excel.WExcel check(byte[] bytes, Validator validator) throws TemplateNotMatchException, ReadErrorException { Workbook workbook = null; InputStream is = null; boolean flag; @@ -245,8 +274,8 @@ public class WSheet implements Serializable, Cloneable { //第一张Sheet表 Sheet sheet = workbook.getSheetAt(0); //获取Sheet名称 - if (clazz.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称 - SheetName sheetName = clazz.getAnnotation(SheetName.class); + if (templateClass.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称 + SheetName sheetName = templateClass.getAnnotation(SheetName.class); this.setName(sheetName.value()); } else {//将类名设为表名,如果类名不存在,将Excel表名设为表名 this.setName(sheet.getSheetName()); @@ -258,12 +287,11 @@ public class WSheet implements Serializable, Cloneable { int columnSum = headRow.getPhysicalNumberOfCells(); //检查列数量 - List list = initColumns(clazz); - if (list.size() != columnSum) { + if (columnList.size() != columnSum) { throw new TemplateNotMatchException("与模板列数量不同。"); } else { - for (int i = 0; i < list.size(); i++) { - WColumn wColumn = list.get(i); + for (int i = 0; i < columnList.size(); i++) { + WColumn wColumn = columnList.get(i); Cell cell = headRow.getCell(i); String headValue = getValue(cell); headValue = headValue.replace("*", ""); @@ -302,7 +330,7 @@ public class WSheet implements Serializable, Cloneable { } try { - T t = clazz.newInstance(); + T t = templateClass.newInstance(); List errs = transferMap(row, t); errs.addAll(ValidationUtil.validate(t)); errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList()); @@ -315,6 +343,7 @@ public class WSheet implements Serializable, Cloneable { row.addError("模板对象默认构造函数错误"); } } + return this; } /** @@ -380,33 +409,27 @@ public class WSheet implements Serializable, Cloneable { this.freezeFirst = freezeFirst; } -// -// public final String toCSV() { -// StringBuilder sb = new StringBuilder(); -// -// for (WColumn WColumn : this.getWColumns()) { -// if (WColumn != null) { -// sb.append(WColumn.getName()).append(","); -// } -// } -// sb.append("\n"); -// -// for (int i = 0; i < this.getRowIndex(); i++) { -// for (int j = 0; j < this.getColumnIndex(); j++) { -// WCell WCell = this.getCell(i, j); -// if (this.columnList.get(j).getExcelType().equals(ExcelType.STRING) -// || this.columnList.get(j).getExcelType().equals(ExcelType.DATE) -// || this.columnList.get(j).getExcelType().equals(ExcelType.DATETIME)) { -// sb.append("\"\t").append(WCell.getValue()).append("\","); -// } else { -// sb.append(WCell.getValue()).append(","); -// } -// } -// sb.append("\n"); -// } -// -// return sb.toString(); -// } + public final String toCSV() { + StringBuilder sb = new StringBuilder(); + + for (WColumn WColumn : this.columnList) { + if (WColumn != null) { + sb.append(WColumn.getName()).append(","); + } + } + sb.append("\n"); + + for (int i = 0; i < this.rowList.size(); i++) { + for (int j = 0; j < this.columnList.size(); j++) { + WColumn wColumn = this.columnList.get(j); + WCell wCell = this.rowList.get(i).get(wColumn.getName()); + sb.append(wCell.getValue()).append(","); + } + sb.append("\n"); + } + + return sb.toString(); + } /** * 判断DataTable是否包含错误信息 @@ -422,18 +445,8 @@ public class WSheet implements Serializable, Cloneable { return false; } - /** - * 得到已生成好的Excel文件的字节流信息 - * - * @return 字节流信息 - * @throws IOException - */ public byte[] getBytes() { - return getBytes(false); - } - - public byte[] getBytes(boolean checkResult) { - XSSFWorkbook workbook = getExcel(checkResult); + XSSFWorkbook workbook = getExcel(); if (workbook != null) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { @@ -446,7 +459,7 @@ public class WSheet implements Serializable, Cloneable { return null; } - public XSSFWorkbook getExcel(boolean checkResult) { + public XSSFWorkbook getExcel() { XSSFWorkbook workbook = new XSSFWorkbook(); //创建一个Sheet表 XSSFSheet sheet = workbook.createSheet(name); @@ -455,13 +468,29 @@ public class WSheet implements Serializable, Cloneable { XSSFDrawing drawing = sheet.createDrawingPatriarch(); int offset = 0; - // 添加错误说明列 - if (checkResult && hasError()) { + // 添加结果和错误说明列 + if (hasError()) { + offset++; + {// 第一栏结果栏 + Cell firstCell = firstRow.createCell(0); + firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle()); + firstCell.setCellValue(new XSSFRichTextString("执行结果")); + XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6); + XSSFComment comment = drawing.createCellComment(anchor); + comment.setString("此列为检查结果,导入时请处理完错误,并删除该列!"); + firstCell.setCellComment(comment); + } offset++; - Cell firstCell = firstRow.createCell(0); - firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle()); - firstCell.setCellValue(new XSSFRichTextString("检查结果说明")); - sheet.setColumnWidth(0, 18 * 256); + {// 第二栏错误信息栏 + Cell secondCell = firstRow.createCell(1); + secondCell.setCellStyle(new HeadCellStyle(workbook).getStyle()); + secondCell.setCellValue(new XSSFRichTextString("结果说明")); + sheet.setColumnWidth(1, 18 * 256); + XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6); + XSSFComment comment = drawing.createCellComment(anchor); + comment.setString("此列为检查结果信息列,导入时请处理完错误,并删除该列!"); + secondCell.setCellComment(comment); + } } for (int j = 0; j < this.columnList.size(); j++) { @@ -499,16 +528,29 @@ public class WSheet implements Serializable, Cloneable { // 填充数据 for (int i = 0; i < this.rowList.size(); i++) { - boolean flag = this.rowList.get(i).hasError(); WRow wRow = this.rowList.get(i); Row row = sheet.createRow(i + 1); - if (flag) { + if (this.rowList.get(i).hasError()) { + // 添加结果 List errorList = wRow.getErrorList(); - Cell xssfCell = row.createCell(0); - xssfCell.setCellStyle(new ErrorCellStyle(workbook).getStyle()); + Cell resultCell = row.createCell(0); + resultCell.setCellStyle(new ErrorCellStyle(workbook).getStyle()); + resultCell.setCellValue("错误"); + // 添加错误信息详细说明 + Cell errsCell = row.createCell(1); + errsCell.setCellStyle(new ErrorCellStyle(workbook).getStyle()); String join = String.join(";", errorList); - xssfCell.setCellValue(new XSSFRichTextString(join)); + errsCell.setCellValue(new XSSFRichTextString(join)); + } else { + // 添加结果 + Cell resultCell = row.createCell(0); + resultCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle()); + resultCell.setCellValue("成功"); + // 添加错误信息详细说明 + Cell errsCell = row.createCell(1); + errsCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle()); + errsCell.setCellValue(""); } for (int j = 0; j < this.columnList.size(); j++) { @@ -565,4 +607,42 @@ public class WSheet implements Serializable, Cloneable { public interface Validator { List validate(T t); } + + public void toFile(File file) throws IOException { + File parentFile = file.getParentFile(); + if (!parentFile.exists() && !parentFile.mkdirs()) { + throw new RuntimeException("路径创建失败"); + } + if (!file.exists() && !file.createNewFile()) { + throw new RuntimeException("文件创建失败"); + } + FileOutputStream fileOutputStream = new FileOutputStream(file); + fileOutputStream.write(getBytes()); + fileOutputStream.close(); + } + + public static void main(String[] args) throws IOException { + Dept dept = new Dept(); + dept.setDeptName("AAAAAA"); + ArrayList depts = new ArrayList(); + depts.add(dept); + File file = new File("E:\\E.xlsx"); + + // 导出模板 +// new WExcel(Dept.class).loadData(depts).toFile(file); + + // 检查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); + check.toFile(new File("E:\\E_err.xlsx")); + System.out.println(check.hasError()); + } catch (TemplateNotMatchException e) { + e.printStackTrace(); + } catch (ReadErrorException e) { + e.printStackTrace(); + } + + System.out.println(); + } } diff --git a/src/main/resources/modules/SpringBoot/java/frame/excel/style/SuccessCellStyle.java b/src/main/resources/modules/SpringBoot/java/frame/excel/style/SuccessCellStyle.java new file mode 100644 index 00000000..136c4744 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/excel/style/SuccessCellStyle.java @@ -0,0 +1,22 @@ +package xyz.wbsite.frame.excel.style; + +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Workbook; + +public class SuccessCellStyle extends BaseCellStyle { + + public SuccessCellStyle(Workbook workbook) { + super(workbook); + style.setFillForegroundColor(HSSFColor.GREEN.index); //背景颜色红色 + style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式 + } + + public CellStyle getStyle() { + return style; + } + + public void setStyle(CellStyle style) { + this.style = style; + } +} diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/ResponseUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/ResponseUtil.java index 0ce3bbfe..c59a23e7 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/utils/ResponseUtil.java +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/ResponseUtil.java @@ -4,7 +4,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.WExcel; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -36,7 +36,7 @@ public class ResponseUtil { } } - public static ResponseEntity apply(WSheet sheet) { + public static ResponseEntity apply(WExcel sheet) { return apply(sheet.getBytes(), sheet.getName() + ".xlsx"); } } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java index bd0c8a95..c2dedb9a 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java @@ -46,7 +46,7 @@ import ${basePackage}.module.system.rsp.TokensUpdateResponse; @Service public class TokensManagerImpl implements xyz.wbsite.module.system.mgr.TokensManager { - @Value("${web.url.auth.admin}") + @Value("${r'${web.url.auth.admin}'}") private String admin; @Autowired private TokensMapper tokensMapper;