异常
使用Alibaba Easy Excel 导出excel 出现LocalDate时间格式异常
com.alibaba.excel.exception.ExcelDataConvertException: Can not find
‘Converter’ support class LocalDate.
问题根因
查看doWrite(List data)的源码时发现Converter接口的convertToExcelData只实现了转换BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、URL几种类型
解决办法
用 converter = LocalDateConverter.class
@ExcelProperty(value=”支付时间”,converter=LocalDateConverter.class)
private LocalDate payDate;
package com.person.common.localdateConver;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**excel 导入 类型转换器yyyy-MM-dd
* @ ExcelProperty(value = "结束时间",converter = LocalDateConverter.class)
* @author zms
* @date 2021-12-15
**/
public class LocalDateConverter implements Converter<LocalDate> {
@Override
public Class supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
@Override
public CellData convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}