Spring Boot 项目导出Excel表格报错Can not find ‘Converter‘ support class Date.
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. 问题解决:
解决方法一:
添加注解:
-
/**
-
* 创建日期
-
*/
-
-
-
-
-
private Date createTime;
但是,需要注意的是时间为 null 时(即数据库中该字段存在为空的数据),这种方法就行不通!导出的 Excel 文件依然没有任何内容。
解决方法二(推荐):
1、自定义一个转换器 LocalDateStringConverter
-
package com.example.excel.utils;
-
-
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.LocalDateTime;
-
import java.time.format.DateTimeFormatter;
-
-
/**
-
* 自定义LocalDateStringConverter
-
* 用于解决使用easyexcel导出表格时候,默认不支持LocalDateTime日期格式
-
*
-
* 在需要的属性上添加注解 @ExcelProperty(value = "创建日期", converter = LocalDateStringConverter.class)
-
*/
-
-
public class LocalDateStringConverter implements Converter<LocalDateTime> {
-
-
public Class supportJavaTypeKey() {
-
return LocalDateTime.class;
-
}
-
-
-
public CellDataTypeEnum supportExcelTypeKey() {
-
return CellDataTypeEnum.STRING;
-
}
-
-
-
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
-
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-
}
-
-
-
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 日期格式,并正确添加注解:
-
-
-
private LocalDateTime createTime;
3、重启服务
-
/**
-
* 导出
-
*/
-
-
public void excelExport(HttpServletResponse response, Driver driver) {
-
List<Driver> list = driverService.list(new QueryWrapper<>(driver));
-
List<DriverExcelVO> driverExcel = DriverExcelWrapper.build().listVO(list);
-
ExcelUtil.export(response, "司机基础信息导出表", "司机基础信息导出表", driverExcel, DriverExcelVO.class);
-
}
测试接口,成功导出 Excel 文件~
注意:
有一个细节也需要注意,如果使用到 wrapper 转换实体,还需要修改对应的实体类中的时间类型为 LocalDateTime,否则导出来的 Excel 表格里面是没有对应的时间数据的!就像这样:
修改对应的实体类的时间类型:
-
/**
-
* 创建日期
-
*/
-
-
private LocalDateTime createTime;
再次导出 Excel 表格,成功~~~~
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgaajae
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24