EasyExcel报错 ExcelDataConvertException: Can not find ‘Converter‘ support class List
业务场景
将一些对象数据导出到excel文件中,其中字符串列表类型的字段要以json字符串的形式导出。例如
public class MyData {@ExcelProperty(value = "a")private Integer a;@ExcelProperty(value = "b")private List<String> b;
}
MyData
类对象的List<String> b
属性要以json字符串的形式输出到excel里:["xxxx","xxxx","xxxx"]
在使用EasyExcel导出时,报错如下:
com.alibaba.excel.exception.ExcelDataConvertException: Can not find 'Converter' support class List.
原因及解决方式
EasyExcel自身实现了一些常用类型的Converter
来支持excel数据到对象的转换,例如 BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、Url。若使用了其他数据类型 或 想定制化数据转换方式,需要实现自己的Converter
,并在使用EasyExcel时注册。
这个业务场景,需要实现将List类型数据与String的转换。实现方式:
第一步,实现Converter
接口
public class ListStringConverter implements Converter<List> {@Overridepublic Class<List> supportJavaTypeKey() {return List.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic List convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {//从excel中读数据时被EasyExcel调用String stringValue = cellData.getStringValue();//用json转换工具将excel单元格中数据转换为java List<String>对象List<String> list = JSONUtil.toList(stringValue, String.class);return list;}@Overridepublic CellData convertToExcelData(List list, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {//写excel文件时被EasyExcel调用//用json转换工具将List对象转换为json字符串String json = JSONUtil.toJsonStr(list);return new CellData(json);}
}
第二步,在导出时,将ListStringConverter
注册
//这里为全局注册,后面会讲到
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), MyData.class).registerConverter(new ListStringConverter()).build();
到此,导出成功
小延伸
LocalDateTime
类型数据比较常用,但EasyExcel自身也不支持。浅实现一下LocalDateTime
的Converter
。public class LocalDateTimeConverter implements Converter<LocalDateTime> {private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@Overridepublic Class<LocalDateTime> supportJavaTypeKey() {return LocalDateTime.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return LocalDateTime.parse(cellData.getStringValue(), DATE_TIME_FORMATTER);}@Overridepublic CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return new CellData<>(value.format(DATE_TIME_FORMATTER));} }
Converter
注册到EasyExcel的方式针对对象的特定字段指定转换器,其他字段不受影响:在字段上使用@ExcelProperty注解,通过设置converter参数的方式注册
public class MyData { @ExcelProperty(value = "b", converter = ListStringConverter.class)private List<String> b;@ExcelProperty(value = "c")private List<String> c; //字段c未配置converter,不会按ListStringConverter指定方式转换 }
全局注册,影响所有相同类型数据的转换
//写excel时注册 ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), MyData.class).registerConverter(new ListStringConverter()).build();//读excel时注册 EasyExcel.read(fileName, MyData.class).registerConverter(new ListStringConverter()).sheet().doRead();
完结撒花✿✿ヽ(°▽°)ノ✿
EasyExcel报错 ExcelDataConvertException: Can not find ‘Converter‘ support class List相关推荐
- 报错:org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for localhos
微服务报错:org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for local ...
- 【springboot】报错Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No conve
[参考] https://www.cnblogs.com/sunupo/p/11905763.html [springboot控制台报错:] WARN 10028 - [nio-8181-exec-3 ...
- EasyExcel报错com.alibaba.excel.exception.ExcelGenerateException: java.lang.ExceptionInInitializerError
硅谷课堂项目BUG记录[01] 一.错误描述: 使用EasyExcel,在执行写操作时报错,并未将查询到的数据写到excel中. 二.控制台报错信息 com.alibaba.excel.excepti ...
- 【错误记录】Mac 中 IntelliJ IDEA 运行 Python 程序报错 ( pip 21.0 will drop support for Python 2.7 in January 20 )
文章目录 一.报错信息 二.解决方案 一.报错信息 在 [错误记录]Mac 中 IntelliJ IDEA 运行 Python 程序报错 ( No module named 'threadpool' ...
- php hugepage,【原创】解决Redis启动报错:Transparent Huge Pages (THP) support enabled in your kernel...
问题背景 Redis启动报错: WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This w ...
- 配置frp报错start error: type [http] not support when vhost_http_port is not set
配置frp 问题描述 服务端配置frpc.ini [common] bind_port = 7000 vhost_http_port = 6001 客户端配置frpc.ini [common] ser ...
- Kafka报错:Magic v1 does not support record headers
今天测试环境出现了一个Kafka发送消息的问题,排查了好长时间才解决,记录一下 问题症状 测试环境突然出现发送Kafka消息失败的错误,错误截图如下: 在网上查一下,很多人都说是Kafka服务端和客户 ...
- GBase 8a在V95版本做节点替换报错single vc mode does not support ‘–freenode’
GBase 8a的V95版本,支持通过空闲节点(freenode)做快速的故障节点替换(省去了找安装包,检查安装配置过程),但freenode只针对开启了VC的场景,对于单VC,也就是兼容V8版本的模 ...
- 【编译报错】start value has mixed support, consider using flex-start instead
背景: 编译less报错 解决办法: 将 justify-content:start; 改为 justify-content:flex-start; 原因: 没有规范的写flex属性所导致的
最新文章
- PDF编辑工具——PDF Desktop Converter 4 Professional
- php多图片上传程序,php多文件上传 多图片上传程序代码
- 渗透测试中的文件传输技巧
- Interview:算法岗位面试—10.10下午—上海某银行算法岗位(偏数据分析,四大行之一)的技术面—关于项目的考察
- 启动maven项目在eclipse中报错 Can‘t find class [×××] for bean with name '××' defined in class path resource
- 全数字实时仿真平台SkyEye与SystemC集成进行时序仿真
- OllyDBG 入门系列(二)-字串参考
- c# npoi 2.5版本设置字体加粗_Python帮你做Excel——格式设置与画图
- java string contains indexof,java.lang.String.contains()方法实例
- Redhat linux AS4 环境下iSCSI协议配置
- c#获取本地ip地址网关子网掩码_C#设置本地网络如DNS、网关、子网掩码、IP等等...
- HTML5的WebGL实现的3D和2D拓扑树
- python做语音信号处理
- 高通Android Q(android10)设置默认锁屏壁纸
- 山西农业大学计算机科学与技术分数线,山西农业大学计算机科学与技术专业2016年在湖北理科高考录取最低分数线...
- 一致性算法之Raft算法
- 扫码反馈,“码”上回复:二维码在业主意见反馈中的应用
- 使用虚拟机VMware 15 pro安装Ubuntu 16.04 LTS
- 【微信小程序】shiro安全登录界面实现
- idea单元测试(导入Junit4的Java包到项目中)