前言
一次使用EasyExcel导出带有LocalDateTime字段报错
解决
定义一个解析类
public class LocalDateStringConverter implements Converter<LocalDateTime> {
@Override
public Class supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Override
public CellData convertToExcelData(LocalDateTime localDateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(localDateTime);
return new CellData(format);
}
}
然后在导出类加上
@Data
public class WhiteListPageExcelDto {
@ExcelProperty(index = 0, value = "用户姓名")
private String userName;
@ExcelProperty(index = 1, value = "手机号")
private String phoneNo;
@ExcelProperty(index = 2, value = "创建时间", converter = LocalDateStringConverter.class)
private LocalDateTime createTime;
}
总结
easyExcel使用可以转换类,可以定制转换需要的字段,不过高版本不会报错