mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-02-01 22:41:56 +08:00
新增枚举字典的Excel导入和导出方式
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.ruoyi.common.core.annotation;
|
||||
|
||||
/**
|
||||
* ===========================================
|
||||
* Copyright 2024 xiaoyang
|
||||
* All rights reserved
|
||||
* <p>项 目 名 :RuoYi-Cloud</p>
|
||||
* <p>文 件 名 :DictTag</p>
|
||||
* <p>描 述 :字典类型</p>
|
||||
*
|
||||
* @author :xiaoyang
|
||||
* @date : 2024/1/12 20:27
|
||||
* ============================================
|
||||
*/
|
||||
public interface DictTag
|
||||
{
|
||||
/**
|
||||
* 绑定键
|
||||
*
|
||||
* @return 绑定的键值
|
||||
*/
|
||||
String getKey();
|
||||
|
||||
/**
|
||||
* 绑定名称
|
||||
*
|
||||
* @return 绑定的名称
|
||||
*/
|
||||
String getName();
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import com.ruoyi.common.core.utils.poi.ExcelHandlerAdapter;
|
||||
|
||||
/**
|
||||
* 自定义导出Excel数据注解
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@@ -37,6 +37,14 @@ public @interface Excel
|
||||
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
|
||||
*/
|
||||
public String readConverterExp() default "";
|
||||
/**
|
||||
* 字典转换器的枚举,该枚举需要实现DictTag标签,否则不生效
|
||||
* <p>
|
||||
* 枚举字典有值时,优先取枚举字典
|
||||
*
|
||||
* @return 字典转换枚举
|
||||
*/
|
||||
Class<? extends Enum> readConverterEnum() default Enum.class;
|
||||
|
||||
/**
|
||||
* 分隔符,读取字符串组内容
|
||||
@@ -179,4 +187,4 @@ public @interface Excel
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import com.ruoyi.common.core.annotation.DictTag;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.RegExUtils;
|
||||
import org.apache.commons.lang3.reflect.FieldUtils;
|
||||
@@ -298,12 +299,30 @@ public class ExcelUtil<T>
|
||||
*/
|
||||
public Map<String,String> initValueToFieldConversionMap(Excel attr){
|
||||
HashMap<String, String> conversionMap = new HashMap<>();
|
||||
String allList = attr.readConverterExp();
|
||||
String[] split = allList.split(",");
|
||||
for (String dictTagItem : split)
|
||||
//如果存在转换exp,提取转换exp
|
||||
if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
||||
{
|
||||
String[] split1 = dictTagItem.split("=");
|
||||
conversionMap.put(split1[1], split1[0]);
|
||||
String allList = attr.readConverterExp();
|
||||
String[] split = allList.split(",");
|
||||
for (String dictTagItem : split)
|
||||
{
|
||||
String[] split1 = dictTagItem.split("=");
|
||||
conversionMap.put(split1[1], split1[0]);
|
||||
}
|
||||
}
|
||||
//字段转换初始化(通过枚举字段转换)
|
||||
if (!attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||
{
|
||||
Class<? extends Enum> aClass = attr.readConverterEnum();
|
||||
Object[] enumConstants = aClass.getEnumConstants();
|
||||
for (Object enumItem : enumConstants)
|
||||
{
|
||||
if (enumItem instanceof DictTag)
|
||||
{
|
||||
DictTag dictTag = (DictTag) enumItem;
|
||||
conversionMap.put(dictTag.getName(), dictTag.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
return conversionMap;
|
||||
}
|
||||
@@ -361,7 +380,9 @@ public class ExcelUtil<T>
|
||||
{
|
||||
fieldsMap.put(column, objects);
|
||||
//如果存在解析键值映射,则直接初始化
|
||||
if(StringUtils.isNotEmpty(attr.readConverterExp())){
|
||||
if(StringUtils.isNotEmpty(attr.readConverterExp())
|
||||
|| !attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||
{
|
||||
Map<String, String> fieldConversionMap = initValueToFieldConversionMap(attr);
|
||||
allFieldConversionMap.put(attr.name(),fieldConversionMap);
|
||||
}
|
||||
@@ -815,6 +836,22 @@ public class ExcelUtil<T>
|
||||
}
|
||||
fieldConversionMap.put(column, conversionMap);
|
||||
}
|
||||
//字段转换初始化(通过枚举字段转换)
|
||||
if (!attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||
{
|
||||
HashMap<String, String> conversionMap = new HashMap<>();
|
||||
Class<? extends Enum> aClass = attr.readConverterEnum();
|
||||
Object[] enumConstants = aClass.getEnumConstants();
|
||||
for (Object enumItem : enumConstants)
|
||||
{
|
||||
if (enumItem instanceof DictTag)
|
||||
{
|
||||
DictTag dictTag = (DictTag) enumItem;
|
||||
conversionMap.put(dictTag.getKey(), dictTag.getName());
|
||||
}
|
||||
}
|
||||
fieldConversionMap.put(column, conversionMap);
|
||||
}
|
||||
if (isSubList())
|
||||
{
|
||||
// 填充默认样式,防止合并单元格样式失效
|
||||
|
||||
Reference in New Issue
Block a user