EasyExcel自定义字段对象转换器支持转换实体和集合实体

文章目录

      • [1. 实现ObjectConverter](#1. 实现ObjectConverter)
      • [2. 使用](#2. 使用)
      • [3. 测试](#3. 测试)
          • [3.2 导出excel](#3.2 导出excel)
          • [3.1 导入excel](#3.1 导入excel)

1. 实现ObjectConverter

复制代码
package com.tophant.cloud.common.excel.converters;

import cn.hutool.json.JSONUtil;
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 java.lang.reflect.Type;

/**
 * 对象转换器
 * 支持转换字段类型为自定义实体,List<实体>,List<String>,Map<String,实体>等
 *
 * @author wan.fei
 * @date 2023/08/26
 */
public class ObjectConverter implements Converter<Object> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 转换从excel中读取的数据为ExcelVO中定义的字段类型(自定义实体,List<实体>,List<String>,Map<String,实体>等)
     *
     * @param cellData            单元格数据
     * @param contentProperty     内容属性
     * @param globalConfiguration 全局配置
     * @return {@link Object}
     * @throws Exception 异常
     */
    @Override
    public Object convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        // 获取定义的实体字段的实际类型,包括泛型参数信息
        Type genericType = contentProperty.getField().getGenericType();
        return JSONUtil.toBean(stringValue, genericType, false);
    }

    /**
     * 转换数据为json字符串,写入到excel文件
     *
     * @param value               价值
     * @param contentProperty     内容属性
     * @param globalConfiguration 全局配置
     * @return {@link WriteCellData}<{@link ?}>
     * @throws Exception 异常
     */
    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws Exception {
        String json = JSONUtil.toJsonStr(value);
        return new WriteCellData<String>(json);
    }
}

2. 使用

3. 测试

3.2 导出excel

手动添加一些数据

导出

写入excel转换成功

3.1 导入excel

将上面生成的excel文件导入

读取excel数据并转换成功

相关推荐
better_liang22 分钟前
每日Java面试场景题知识点之-XXL-JOB分布式任务调度实践
java·spring boot·xxl-job·分布式任务调度·企业级开发
会游泳的石头24 分钟前
一行注解防死循环:MyBatis 递归深度限制(无需 level 字段)
java·mybatis
q***o37625 分钟前
Spring Boot环境配置
java·spring boot·后端
oMcLin27 分钟前
如何在SUSE Linux Enterprise Server 15 SP4上通过配置并优化ZFS存储池,提升文件存储与数据备份的效率?
java·linux·运维
TaiKuLaHa40 分钟前
Spring Bean的生命周期
java·后端·spring
刀法如飞1 小时前
开箱即用的 DDD(领域驱动设计)工程脚手架,基于 Spring Boot 4.0.1 和 Java 21
java·spring boot·mysql·spring·设计模式·intellij-idea
我是苏苏1 小时前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端
JavaGuide1 小时前
SpringBoot 官宣停止维护 3.2.x~3.4.x!
java·后端
tkevinjd2 小时前
动态代理
java