java使用EasyExcel生成表格及Can not find ‘Converter‘ support class LocalDateTime错误

这篇博客介绍了如何利用EasyExcel库在Java中便捷地导出数据到Excel文件。首先展示了调用方法,通过设置响应头和使用EasyExcel.write()方法来写入数据。接着,讲解了在实体类属性上添加@ExcelProperty注解以指定字段名和索引,以及处理LocalDateTime类型的转换,需要自定义一个LocalDateTimeConverter类进行转换。
摘要由CSDN通过智能技术生成

java使用EasyExcel生成表格

一、调用方法

public static <E> void exportList(HttpServletResponse response, String fileName,String sheetName,List<E> list, Class<E> tClass) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + "." + "xls");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
          
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            EasyExcel.write(response.getOutputStream(), tClass).excelType(ExcelTypeEnum.XLS)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(StringUtils.isNotBlank(sheetName) ? sheetName :"Sheet1").doWrite(list);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

二、在主要的实体类属性上添加注解

@ExcelProperty(value = "姓名", index = 0)
@@ColumnWidth(value = 30)

三、关于实体类属性类型为LocalDateTime转换(注意EasyExcel是支持Date类型的)

报错:
Can not find 'Converter' support class LocalDateTime.

解决:编写一个转换类,并作用于类对应属性上

 @ExcelProperty(value = "发送时间",index = 5,converter = LocalDateTimeConverter.class)
    @ColumnWidth(value = 20)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;
public class LocalDateTimeConverter implements Converter<LocalDateTime> {

    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值