From 352caa3e9386cf95c58fafa75a5c4d4bfa0d9a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 1 Aug 2023 13:14:26 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20excel=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E5=AD=97=E5=85=B8=E8=BD=AC=E4=B8=8B=E6=8B=89=E6=A1=86?= =?UTF-8?q?=20=E6=97=A0=E9=9C=80=E6=A0=87=E8=AE=B0index=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86(=E6=84=9F=E8=B0=A2=E4=B8=80=E5=A4=8Fcoco)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/core/ExcelDownHandler.java | 26 +++++++++---------- .../dromara/system/domain/vo/SysClientVo.java | 2 +- .../dromara/system/domain/vo/SysConfigVo.java | 2 +- .../dromara/system/domain/vo/SysDeptVo.java | 2 +- .../system/domain/vo/SysDictDataVo.java | 4 +-- .../system/domain/vo/SysDictTypeVo.java | 2 +- .../system/domain/vo/SysLogininforVo.java | 2 +- .../system/domain/vo/SysOperLogVo.java | 6 ++--- .../dromara/system/domain/vo/SysPostVo.java | 2 +- .../dromara/system/domain/vo/SysRoleVo.java | 4 +-- .../system/domain/vo/SysTenantPackageVo.java | 2 +- .../dromara/system/domain/vo/SysTenantVo.java | 2 +- .../system/domain/vo/SysUserExportVo.java | 4 +-- 13 files changed, 29 insertions(+), 31 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/ExcelDownHandler.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/ExcelDownHandler.java index db4fecca..3b791ea5 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/ExcelDownHandler.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/ExcelDownHandler.java @@ -5,7 +5,9 @@ import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.EnumUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.metadata.FieldCache; +import com.alibaba.excel.metadata.FieldWrapper; +import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; @@ -83,16 +85,18 @@ public class ExcelDownHandler implements SheetWriteHandler { Sheet sheet = writeSheetHolder.getSheet(); // 开始设置下拉框 HSSFWorkbook DataValidationHelper helper = sheet.getDataValidationHelper(); - Field[] fields = writeWorkbookHolder.getClazz().getDeclaredFields(); Workbook workbook = writeWorkbookHolder.getWorkbook(); - int length = fields.length; - for (int i = 0; i < length; i++) { + FieldCache fieldCache = ClassUtils.declaredFields(writeWorkbookHolder.getClazz(), writeWorkbookHolder); + for (Map.Entry entry : fieldCache.getSortedFieldMap().entrySet()) { + Integer index = entry.getKey(); + FieldWrapper wrapper = entry.getValue(); + Field field = wrapper.getField(); // 循环实体中的每个属性 // 可选的下拉值 List options = new ArrayList<>(); - if (fields[i].isAnnotationPresent(ExcelDictFormat.class)) { + if (field.isAnnotationPresent(ExcelDictFormat.class)) { // 如果指定了@ExcelDictFormat,则使用字典的逻辑 - ExcelDictFormat format = fields[i].getDeclaredAnnotation(ExcelDictFormat.class); + ExcelDictFormat format = field.getDeclaredAnnotation(ExcelDictFormat.class); String dictType = format.dictType(); String converterExp = format.readConverterExp(); if (StrUtil.isNotBlank(dictType)) { @@ -105,20 +109,14 @@ public class ExcelDownHandler implements SheetWriteHandler { // 如果指定了确切的值,则直接解析确切的值 options = StrUtil.split(converterExp, format.separator(), true, true); } - } else if (fields[i].isAnnotationPresent(ExcelEnumFormat.class)) { + } else if (field.isAnnotationPresent(ExcelEnumFormat.class)) { // 否则如果指定了@ExcelEnumFormat,则使用枚举的逻辑 - ExcelEnumFormat format = fields[i].getDeclaredAnnotation(ExcelEnumFormat.class); + ExcelEnumFormat format = field.getDeclaredAnnotation(ExcelEnumFormat.class); List values = EnumUtil.getFieldValues(format.enumClass(), format.textField()); options = StreamUtils.toList(values, String::valueOf); } if (ObjectUtil.isNotEmpty(options)) { // 仅当下拉可选项不为空时执行 - // 获取列下标,默认为当前循环次数 - int index = i; - if (fields[i].isAnnotationPresent(ExcelProperty.class)) { - // 如果指定了列下标,以指定的为主 - index = fields[i].getDeclaredAnnotation(ExcelProperty.class).index(); - } if (options.size() > 20) { // 这里限制如果可选项大于20,则使用额外表形式 dropDownWithSheet(helper, workbook, sheet, index, options); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysClientVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysClientVo.java index fa23cd1b..34f24eb3 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysClientVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysClientVo.java @@ -82,7 +82,7 @@ public class SysClientVo implements Serializable { /** * 状态(0正常 1停用) */ - @ExcelProperty(value = "状态", index = 7, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(readConverterExp = "0=正常,1=停用") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysConfigVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysConfigVo.java index db7d53a3..f896000a 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysConfigVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysConfigVo.java @@ -53,7 +53,7 @@ public class SysConfigVo implements Serializable { /** * 系统内置(Y是 N否) */ - @ExcelProperty(value = "系统内置", index = 4, converter = ExcelDictConvert.class) + @ExcelProperty(value = "系统内置", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_yes_no") private String configType; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDeptVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDeptVo.java index 4611ae0f..e09d67e1 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDeptVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDeptVo.java @@ -78,7 +78,7 @@ public class SysDeptVo implements Serializable { /** * 部门状态(0正常 1停用) */ - @ExcelProperty(value = "部门状态", index = 5, converter = ExcelDictConvert.class) + @ExcelProperty(value = "部门状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictDataVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictDataVo.java index 1372f316..d058d5e9 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictDataVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictDataVo.java @@ -69,14 +69,14 @@ public class SysDictDataVo implements Serializable { /** * 是否默认(Y是 N否) */ - @ExcelProperty(value = "是否默认", index = 5, converter = ExcelDictConvert.class) + @ExcelProperty(value = "是否默认", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_yes_no") private String isDefault; /** * 状态(0正常 1停用) */ - @ExcelProperty(value = "状态", index = 6, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictTypeVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictTypeVo.java index 771f9563..2cac4a25 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictTypeVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysDictTypeVo.java @@ -47,7 +47,7 @@ public class SysDictTypeVo implements Serializable { /** * 状态(0正常 1停用) */ - @ExcelProperty(value = "状态", index = 3, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysLogininforVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysLogininforVo.java index 97bc79b3..cf9720e9 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysLogininforVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysLogininforVo.java @@ -48,7 +48,7 @@ public class SysLogininforVo implements Serializable { /** * 登录状态(0成功 1失败) */ - @ExcelProperty(value = "登录状态", index = 2, converter = ExcelDictConvert.class) + @ExcelProperty(value = "登录状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_common_status") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysOperLogVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysOperLogVo.java index 306237e3..d9eb71d4 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysOperLogVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysOperLogVo.java @@ -47,7 +47,7 @@ public class SysOperLogVo implements Serializable { /** * 业务类型(0其它 1新增 2修改 3删除) */ - @ExcelProperty(value = "业务类型", index = 2, converter = ExcelDictConvert.class) + @ExcelProperty(value = "业务类型", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_oper_type") private Integer businessType; @@ -71,7 +71,7 @@ public class SysOperLogVo implements Serializable { /** * 操作类别(0其它 1后台用户 2手机端用户) */ - @ExcelProperty(value = "操作类别", index = 5, converter = ExcelDictConvert.class) + @ExcelProperty(value = "操作类别", converter = ExcelDictConvert.class) @ExcelDictFormat(readConverterExp = "0=其它,1=后台用户,2=手机端用户") private Integer operatorType; @@ -120,7 +120,7 @@ public class SysOperLogVo implements Serializable { /** * 操作状态(0正常 1异常) */ - @ExcelProperty(value = "状态", index = 13, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_common_status") private Integer status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysPostVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysPostVo.java index 3185423a..3e6c6a84 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysPostVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysPostVo.java @@ -54,7 +54,7 @@ public class SysPostVo implements Serializable { /** * 状态(0正常 1停用) */ - @ExcelProperty(value = "状态", index = 4, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysRoleVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysRoleVo.java index 015a0436..1e5cd9ee 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysRoleVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysRoleVo.java @@ -53,7 +53,7 @@ public class SysRoleVo implements Serializable { /** * 数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限) */ - @ExcelProperty(value = "数据范围", index = 4, converter = ExcelDictConvert.class) + @ExcelProperty(value = "数据范围", converter = ExcelDictConvert.class) @ExcelDictFormat(readConverterExp = "1=所有数据权限,2=自定义数据权限,3=本部门数据权限,4=本部门及以下数据权限,5=仅本人数据权限") private String dataScope; @@ -72,7 +72,7 @@ public class SysRoleVo implements Serializable { /** * 角色状态(0正常 1停用) */ - @ExcelProperty(value = "角色状态", index = 7, converter = ExcelDictConvert.class) + @ExcelProperty(value = "角色状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantPackageVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantPackageVo.java index ae11a820..070334b4 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantPackageVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantPackageVo.java @@ -58,7 +58,7 @@ public class SysTenantPackageVo implements Serializable { /** * 状态(0正常 1停用) */ - @ExcelProperty(value = "状态", index = 5, converter = ExcelDictConvert.class) + @ExcelProperty(value = "状态", converter = ExcelDictConvert.class) @ExcelDictFormat(readConverterExp = "0=正常,1=停用") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantVo.java index 49e03ea3..6a453150 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysTenantVo.java @@ -107,7 +107,7 @@ public class SysTenantVo implements Serializable { /** * 租户状态(0正常 1停用) */ - @ExcelProperty(value = "租户状态", index = 13, converter = ExcelDictConvert.class) + @ExcelProperty(value = "租户状态", converter = ExcelDictConvert.class) @ExcelDictFormat(readConverterExp = "0=正常,1=停用") private String status; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysUserExportVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysUserExportVo.java index b7fc4800..21b1efbe 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysUserExportVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysUserExportVo.java @@ -59,14 +59,14 @@ public class SysUserExportVo implements Serializable { /** * 用户性别 */ - @ExcelProperty(value = "用户性别", index = 6, converter = ExcelDictConvert.class) + @ExcelProperty(value = "用户性别", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_user_sex") private String sex; /** * 帐号状态(0正常 1停用) */ - @ExcelProperty(value = "帐号状态", index = 7, converter = ExcelDictConvert.class) + @ExcelProperty(value = "帐号状态", converter = ExcelDictConvert.class) @ExcelDictFormat(dictType = "sys_normal_disable") private String status;