EasyExcel 导出字段踩坑
问题 1:数组类型错误
报错
com.alibaba.excel.exception.ExcelWriteDataConvertException: Can not find 'Converter' support class List.
原因
EasyExcel Converter 接口的convertToExcelData
只实现了转换 BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、URL 这些类型,意味着参数 data 最多只能是个二维数据,当需要转换 List
解决:补充转化器
@Component public class ListConvert implements Converter<List> { @Override public WriteCellData<?> convertToExcelData(List value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { StringBuilder stringBuilder = new StringBuilder(); value.forEach(o -> stringBuilder.append(o).append(",")); return new WriteCellData<>(CellDataTypeEnum.STRING, stringBuilder.toString()); } @Override public List convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return new ArrayList<>(Arrays.asList(cellData.getStringValue().split(","))); } @Override public Class<?> supportJavaTypeKey() { return List.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } }
@Schema(description = "方案ID") @ExcelProperty(value = "方案ID", converter = ListConvert.class) private List<Long> bizPlanIds;
问题 2:日期类型未正常显式
问题描述
导出后 Excel 文件日期列 显示 ############
,手动调整单元格类型为日期后正常显示内容
解决:补充转换器
package com.tbit.cloud.framework.excel.core.convert; 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.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Component public class LocalDateTimeConvert implements Converter<LocalDateTime> { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { if (value == null) { return new WriteCellData<>(CellDataTypeEnum.STRING, ""); } // 格式化 LocalDateTime 为字符串 String formattedDate = value.format(DATE_TIME_FORMATTER); return new WriteCellData<>(CellDataTypeEnum.STRING, formattedDate); } @Override public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { String dateString = cellData.getStringValue(); // 转换字符串为 LocalDateTime return LocalDateTime.parse(dateString, DATE_TIME_FORMATTER); } @Override public Class<?> supportJavaTypeKey() { return LocalDateTime.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } }
@Schema(description = "登录时间", requiredMode = Schema.RequiredMode.REQUIRED) @ExcelProperty(value = "登录时间", converter = LocalDateTimeConvert.class) private LocalDateTime createTime;
本文作者:YaosGHC
本文链接:https://www.cnblogs.com/yaocy/p/18728938
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库