0、EasyExcel自定义转换器Converter

一、问题描述

使用 EasyExcel 在导出某个业务数据时抛:

Can not find 'Converter' support class Timestamp.

com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.doConvert(AbstractExcelWriteExecutor.java:323)

项目使用的 EasyExcel 版本号:


  com.alibaba
  easyexcel
  3.0.2

二、 问题产生原因

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{

	@Override
    public Class supportJavaTypeKey() {
        return Timestamp.class;
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

	@Override
	public WriteCellData convertToExcelData(Timestamp timestamp, ExcelContentProperty contentProperty,
			GlobalConfiguration globalConfiguration) throws Exception {
		return new WriteCellData(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> productList = 查询数据数据

// productList 如果数据量很大一定要做分页查询,避免占用内存过大
excelWriterSheetBuilder.doFill(productList);

资料参考:

EasyExcel 自定义LocalDate类型转换器Converter_旭东怪的博客-CSDN博客_easyexcel自定义转换器

你可能感兴趣的:(EasyExcel,EasyExcel定义转换器,Converter)