Can not find ‘Converter‘ support class LocalDateTime

在使用easyexcel进行表格导出的时候出现错误:

com.alibaba.excel.exception.ExcelDataConvertException: Can not find 'Converter' support class LocalDateTime.

在springboot项目中使用使用easyexcel,默认是支持Date日期格式的导出的,但是不支持LocalDateTime日期格式:

解决方案:

1、这时候需要自定义一个LocalDateStringConverter

/**
 * 自定义LocalDateStringConverter
 * 用于解决使用easyexcel导出表格时候,默认不支持LocalDateTime日期格式
 *
 * 在需要的属性上添加注解 @ExcelProperty(value = "入职时间", converter = LocalDateStringConverter.class)
 */

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);
    }
}

2、在对应的localDateTime日期格式添加注解:

//@DateTimeFormat("yyyy-MM-dd") //导入阿里巴巴的包,适用于date格式日期,自定义日期的格式
@ExcelProperty(value = "入职时间", converter = LocalDateStringConverter.class)
private LocalDateTime createTime;

3、在controller中调用,以文件下载的形式进行表格的导出

 @GetMapping("export")
    public ResponseEntity<byte[]> exportAdminInfo() throws UnsupportedEncodingException {
        List<Admin> list = adminService.findAll();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        EasyExcel.write(outputStream, Admin.class).sheet("员工信息表").doWrite(list);

        byte[] bytes = outputStream.toByteArray();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachement", URLEncoder.encode("员工信息表.xlsx", "utf-8"));

        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
    }
  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这个报错是由于在使用EasyExcel库时,没有找到支持LocalDateTime类型的转换器。解决这个问题的方法是在对应的LocalDateTime日期格式属性上添加注解,并指定一个转换器。例如,可以使用@ExcelProperty注解来指定转换器,如下所示: ```java @ExcelProperty(value = "开始时间", converter = LocalDateStringConverter.class) private LocalDateTime startTime; ``` 其中,LocalDateStringConverter是一个自定义的转换器类,用于将LocalDateTime类型转换为字符串。通过这样的方式,EasyExcel库就能正确地处理LocalDateTime类型的属性了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java使用EasyExcel生成表格及Can not find ‘Convertersupport class LocalDateTime错误](https://blog.csdn.net/weixin_43364428/article/details/119915620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot使用excel表格导出数据时 Can not find ‘Convertersupport class LocalDateTime](https://blog.csdn.net/weixin_53815644/article/details/123085927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值