mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、新增BaseEnum、EnumConvert、EnumFormat:支持excel导出从枚举获取描述;2、购买记录、组织余额excel导出文件内容修正;
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
package com.cf.imes.framework.excel.core.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 字典格式化注解
|
||||
*
|
||||
* 实现将枚举的魔值,格式化成枚举的描述
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/8/27 15:02
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface EnumFormat {
|
||||
/**
|
||||
* 对应的枚举类型
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Class<? extends Enum> value();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.cf.imes.framework.excel.core.convert;
|
||||
|
||||
/**
|
||||
* 基础枚举接口
|
||||
* <p>
|
||||
* 用于easyexcel convert
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/8/27 11:51
|
||||
*/
|
||||
public interface BaseEnum {
|
||||
/**
|
||||
* 魔值
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Integer getCode();
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getDesc();
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.cf.imes.framework.excel.core.convert;
|
||||
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.converters.ReadConverterContext;
|
||||
import com.alibaba.excel.converters.WriteConverterContext;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.cf.imes.framework.excel.core.annotations.EnumFormat;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* Excel 枚举转换器
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/8/27 11:02
|
||||
*/
|
||||
@Slf4j
|
||||
public class EnumConvert implements Converter<Integer> {
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return Integer.class; // 泛型接口
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
return CellDataTypeEnum.STRING; // Excel 存字符串
|
||||
}
|
||||
|
||||
// 写出:Integer -> 枚举描述
|
||||
@Override
|
||||
public WriteCellData<String> convertToExcelData(WriteConverterContext<Integer> context) {
|
||||
Integer code = context.getValue();
|
||||
if (code == null) {
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
Class<? extends Enum<?>> enumClass = getEnumClass(context.getContentProperty().getField());
|
||||
if (enumClass != null) {
|
||||
for (Object e : enumClass.getEnumConstants()) {
|
||||
BaseEnum baseEnum = (BaseEnum) e;
|
||||
if (baseEnum.getCode().equals(code)) {
|
||||
return new WriteCellData<>(baseEnum.getDesc());
|
||||
}
|
||||
}
|
||||
}
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
|
||||
// 读入:枚举描述 -> Integer code
|
||||
@Override
|
||||
public Integer convertToJavaData(ReadConverterContext<?> context) {
|
||||
String desc = context.getReadCellData().getStringValue();
|
||||
if (desc == null) {
|
||||
return null;
|
||||
}
|
||||
Class<? extends Enum<?>> enumClass = getEnumClass(context.getContentProperty().getField());
|
||||
if (enumClass != null) {
|
||||
for (Object e : enumClass.getEnumConstants()) {
|
||||
BaseEnum baseEnum = (BaseEnum) e;
|
||||
if (baseEnum.getDesc().equals(desc)) {
|
||||
return baseEnum.getCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Class<? extends Enum<?>> getEnumClass(Field field) {
|
||||
EnumFormat annotation = field.getAnnotation(EnumFormat.class);
|
||||
return annotation != null ? annotation.value() : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user