EasyExcel 自定义字典值的转换器

1,261 阅读2分钟

1、easyexcel 对应的依赖版本

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel-core</artifactId>
    <version>3.3.2</version>
    <scope>compile</scope>
</dependency>

注意:早期版本的方法处理 单元值 为 CellData ,现版本 分了读写两个 ReadCellDataWriteCellData,都继承于前者。

2、提供 SpringContextHolder 用于在 类中 获取 bean 对象处理 字典值

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author frank
 * @version 1.0
 * @description: 类中获取bean,非注入
 * @date 2024年04月01日
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return SpringContextHolder.applicationContext;
    }

    public static <T> T getBean(String beanName) {
        return (T) SpringContextHolder.applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> typeName) {
        return applicationContext.getBean(typeName);
    }

}

3、定义 注解 ,标识对应字典类

import java.lang.annotation.*;

/**
 * excel字典转换注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface ExcelDictItem {
    /**
     * 字典type
     */
    String type();
}

4、定义转换器

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.text.ParseException;

/**
 * @author frank
 * @version 1.0
 * @description: Excel Converter字典转换器
 * @date 2024年04月01日
 */
public class ExcelDictConverter implements Converter<String> {
    public ExcelDictConverter() {
    }

    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    /**
     * 将excel单元格数据转换成对象属性值   name-->code
     *
     * @param cellData             单元格的数据
     * @param excelContentProperty excel每一行的数据内容
     * @param globalConfiguration  global全局配置
     * @return 转换后的对象属性值
     * @throws Exception 异常
     */
    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
        // 获取字典类型
        Field field = excelContentProperty.getField();
        ExcelDictItem excel = field.getAnnotation(ExcelDictItem.class);
        String dictType = excel.type();

        // 为空返回
        String dictLabel = cellData.getStringValue();
        if (StringUtils.isBlank(dictLabel)) {
            return dictLabel;
        }


        //根据当前字典值---》名称
        SpringContextHolder.getBean("相关外部bean");

        return String.valueOf("dictValues");
    }


    /**
     * 将对象属性值转换成excel单元格数据 code-->name
     *
     * @param dictValue            对象的属性值
     * @param excelContentProperty excel每一行的数据内容
     * @param globalConfiguration  global全局配置
     * @return 转换后的对象属性值
     * @throws Exception 异常
     */
    @Override
    public WriteCellData<?> convertToExcelData(String dictValue, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        // 获取字典类型
        Field field = excelContentProperty.getField();
        ExcelDictItem excel = field.getAnnotation(ExcelDictItem.class);
        String dictType = excel.type();
        // 为空返回
        if (StringUtils.isBlank(dictValue)) {
            return new WriteCellData(dictValue);
        }
        SpringContextHolder.getBean("相关外部bean");
        // 查询字典
        //根据当前---》名称---》字典值
        return new WriteCellData("String.valueOf()");
    }

}

5、属性定义

/**
 * 性别
 */
@ExcelProperty(value = "性别", converter = ExcelDictConverter.class)
@ExcelDictItem(type = "字典类型")
private String sex;