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数据并转换成功

相关推荐
大筒木老辈子3 分钟前
C++笔记---并发支持库(atomic)
java·c++·笔记
Cricyta Sevina3 分钟前
Java Collection 集合进阶知识笔记
java·笔记·python·collection集合
BD_Marathon12 分钟前
【JavaWeb】Servlet_url-pattern的一些特殊写法问题
java·开发语言·servlet
黄俊懿14 分钟前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——开启全局事务
java·数据库·spring·spring cloud·微服务·架构·架构师
零度@24 分钟前
Java中Map的多种用法
java·前端·python
中文很快乐25 分钟前
java开发--开发工具全面介绍--新手养成记
java·开发语言·java开发·开发工具介绍·idea开发工具
yaoxin52112342 分钟前
268. Java Stream API 入门指南
java·开发语言·python
ss2731 小时前
ConcurrentLinkedQueue实战:电商秒杀系统的队列选型优化
java·开发语言·安全
BD_Marathon1 小时前
【JavaWeb】Servlet_jar包导入和Content-Type问题
java·servlet·jar
hgz07103 小时前
JMeter性能压测执行与Linux环境部署
java·linux·jmeter