mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 excel导出字典转下拉框 无需标记index自动处理(感谢一夏coco)
This commit is contained in:
		@@ -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<Integer, FieldWrapper> entry : fieldCache.getSortedFieldMap().entrySet()) {
 | 
			
		||||
            Integer index = entry.getKey();
 | 
			
		||||
            FieldWrapper wrapper = entry.getValue();
 | 
			
		||||
            Field field = wrapper.getField();
 | 
			
		||||
            // 循环实体中的每个属性
 | 
			
		||||
            // 可选的下拉值
 | 
			
		||||
            List<String> 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<Object> 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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user