一、问题描述
使用 EasyExcel 在导出某个业务数据时抛:
Can not find 'Converter' support class Timestamp.
com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.doConvert(AbstractExcelWriteExecutor.java:323)
项目使用的 EasyExcel 版本号:
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.0.2</version></dependency>
二、 问题产生原因
EasyExcel 没有定义Timestamp 类型导出数据处理。
三、解决方案
定义对应的Timestamp 处理类型即可
3.1、自定义 Timestamp 处理类
package com.yandype.util.easyExcel;import java.sql.Timestamp;import java.text.SimpleDateFormat;import com.alibaba.excel.converters.Converter;import com.alibaba.excel.enums.CellDataTypeEnum;import com.alibaba.excel.metadata.GlobalConfiguration;import com.alibaba.excel.metadata.data.WriteCellData;import com.alibaba.excel.metadata.property.ExcelContentProperty;public class TimestampConverter implements Converter<Timestamp>{@Overridepublic Class<Timestamp> supportJavaTypeKey() {return Timestamp.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic WriteCellData<String> convertToExcelData(Timestamp timestamp, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) throws Exception {return new WriteCellData<String>(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp));}}
3.2、注册该自定义Timestamp 处理类
File uploadFile = File.createTempFile("export", ".xlsx");String templateFilePath = systemUrl + "/template/exportPublishShop.xlsx";ExcelWriterSheetBuilder excelWriterSheetBuilder = EasyExcel.write(uploadFile).registerConverter(new TimestampConverter()).withTemplate(templateFilePath).sheet();List<Map<String, String>> productList = 查询数据数据// productList 如果数据量很大一定要做分页查询,避免占用内存过大excelWriterSheetBuilder.doFill(productList);
资料参考:
EasyExcel 自定义LocalDate类型转换器Converter_旭东怪的博客-CSDN博客_easyexcel自定义转换器