• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Boot 项目导出Excel表格报错Can not find ‘Converter‘ support class Date.

武飞扬头像
破风_1874
帮助1

1. 问题描述:

Spring Boot 项目导出 Excel 表格

引入的依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.6</version>
</dependency>

启动服务,调用接口,成功导出 Excel文件,但是文件没有数据,且报错:Can not find ‘Converter‘ support class Date.

学新通

2. 原因分析:

点击报错,定位到创建时间字段,剖析报错原因:使用 easyexcel 导出 Excel 表格时候,默认不支持 DateTime 日期格式,所以需要指定 DateTime 类型的字段的日期格式。

3. 问题解决:

解决方法一:

添加注解:

  1.  
    /**
  2.  
    * 创建日期
  3.  
    */
  4.  
    @ColumnWidth(20)
  5.  
    @ExcelProperty(value = "创建日期")
  6.  
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  7.  
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  8.  
    private Date createTime;

 但是,需要注意的是时间为 null 时(即数据库中该字段存在为空的数据),这种方法就行不通!导出的 Excel 文件依然没有任何内容。

解决方法二(推荐):

1、自定义一个转换器 LocalDateStringConverter

  1.  
    package com.example.excel.utils;
  2.  
     
  3.  
    import com.alibaba.excel.converters.Converter;
  4.  
    import com.alibaba.excel.enums.CellDataTypeEnum;
  5.  
    import com.alibaba.excel.metadata.CellData;
  6.  
    import com.alibaba.excel.metadata.GlobalConfiguration;
  7.  
    import com.alibaba.excel.metadata.property.ExcelContentProperty;
  8.  
     
  9.  
    import java.time.LocalDateTime;
  10.  
    import java.time.format.DateTimeFormatter;
  11.  
     
  12.  
    /**
  13.  
    * 自定义LocalDateStringConverter
  14.  
    * 用于解决使用easyexcel导出表格时候,默认不支持LocalDateTime日期格式
  15.  
    *
  16.  
    * 在需要的属性上添加注解 @ExcelProperty(value = "创建日期", converter = LocalDateStringConverter.class)
  17.  
    */
  18.  
     
  19.  
    public class LocalDateStringConverter implements Converter<LocalDateTime> {
  20.  
    @Override
  21.  
    public Class supportJavaTypeKey() {
  22.  
    return LocalDateTime.class;
  23.  
    }
  24.  
     
  25.  
    @Override
  26.  
    public CellDataTypeEnum supportExcelTypeKey() {
  27.  
    return CellDataTypeEnum.STRING;
  28.  
    }
  29.  
     
  30.  
    @Override
  31.  
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  32.  
    return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  33.  
    }
  34.  
     
  35.  
    @Override
  36.  
    public CellData convertToExcelData(LocalDateTime localDateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  37.  
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  38.  
    String format = formatter.format(localDateTime);
  39.  
    return new CellData(format);
  40.  
    }
  41.  
    }
学新通

2、修改字段类型为 LocalDateTime 日期格式,并正确添加注解:

  1.  
    @ColumnWidth(20)
  2.  
    @ExcelProperty(value = "创建日期", converter = LocalDateStringConverter.class)
  3.  
    private LocalDateTime createTime;

3、重启服务

  1.  
    /**
  2.  
    * 导出
  3.  
    */
  4.  
    @PostMapping("/excelExport")
  5.  
    public void excelExport(HttpServletResponse response, Driver driver) {
  6.  
    List<Driver> list = driverService.list(new QueryWrapper<>(driver));
  7.  
    List<DriverExcelVO> driverExcel = DriverExcelWrapper.build().listVO(list);
  8.  
    ExcelUtil.export(response, "司机基础信息导出表", "司机基础信息导出表", driverExcel, DriverExcelVO.class);
  9.  
    }

测试接口,成功导出 Excel 文件~

学新通

 注意:

有一个细节也需要注意,如果使用到 wrapper 转换实体,还需要修改对应的实体类中的时间类型为 LocalDateTime,否则导出来的 Excel 表格里面是没有对应的时间数据的!就像这样:

学新通

 修改对应的实体类的时间类型:

  1.  
    /**
  2.  
    * 创建日期
  3.  
    */
  4.  
    @TableField("create_time")
  5.  
    private LocalDateTime createTime;

再次导出 Excel 表格,成功~~~~ 

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaajae
系列文章
更多 icon
同类精品
更多 icon
继续加载