如何解決EasyExcel導出文件LocalDateTime報錯問題
解決EasyExcel導出文件LocalDateTime報錯
問題引出
我在參與一個項目的時候接觸到數據表格導出為Excel表格的需求,但是在導出的時候會出現(xiàn)報錯
Cannot find ‘Converter’ support class LocalDateTime
原因是我需要導出的實體類中存在 LocalDateTime 類型的屬性,而又恰巧 EasyExcel 不支持 LocalDate 和 LocalDateTime 接收數據,啊人生。
解決方案
在尋找了多篇文章之后,在下面對這個問題進行總結,既然默認不支持我使用這個類型,那就殺出一條路來。
首先先來看報的錯誤提示:
Cannot find ‘Converter’ support class LocalDateTime
明顯可以看出來是找不到 LocalDateTime 的一個 Converter,那么這個所謂的 Converter 到底是個什么東西呢?既然他缺少這個東西我們能不能給他自己弄一個上去呢?說干就干!(其實解決方法也真就是這個)
自定義Converter
Converter 在這里其實是一個字段轉換器,在 EasyExcel 中擔任將Java屬性轉換成Excel表格中合法數據的一個小東西。
由于EasyExcel自己沒有我們需要的 LocalDateTime 的字段轉換器,那我們就自己搞一個出來。
在 config 包下新建一個自定義的字段轉換器 LocalDateTimeConverter 。
package edu.lingnan.rili.converter;
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;
/**
* @author xBaozi
* @version 1.0
* @classname LocalDateTimeConverter
* @description EasyExcel LocalDateTime轉換器
* @date 2022/3/19 2:17
*/
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
@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(DEFAULT_PATTERN));
}
@Override
public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData<>(value.format(DateTimeFormatter.ofPattern(DEFAULT_PATTERN)));
}
}引用 LocalDateTimeConverter
既然這東西是本來沒有然后我們自己弄出來的,那肯定要告訴程序:**誒!這里我給你生了個崽,要記得領回去?。?*所以,我們在原本需要導出的 LocalDateTime 類型中的 @ExcelProperty 注解中引入自定義的字段轉換器。
同時要加上一個JsonFormat的格式化注解,將該屬性轉換成 json格式,這里要注意的就是要導入一下阿里巴巴的 fastjson Maven坐標了。
@ExcelProperty(value = "操作時間", index = 8, converter = LocalDateTimeConverter.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("操作時間")
private LocalDateTime operationTime;<!-- fastjson的Maven坐標 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>好啦!到這里我們遇到的問題就已經解決了呀!雖然說前期剛遇到這個問題的時候一直在被折磨,但是卻是一個很好的一個提升機會,沒點兒bug,怎么能保得住頭發(fā)呢是吧。
EasyExcel導出問題
報錯信息?。。?/h3>
錯誤一:
com.alibaba.excel.exception.ExcelAnalysisException: java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.beans.BeanMap$Generator
com.alibaba.excel.exception.ExcelAnalysisException: java.lang.VerifyError: class net.sf.cglib.core.DebuggingClassWriter overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V

上面兩種報錯?。?!都是一個原因,各種查詢之后,說是版本沖突,這種就很頭疼
經過排查,我在pom中將單元測試的依賴注解之后,導入就正常了

錯誤二:
com.alibaba.excel.exception.ExcelDataConvertException: Can not find 'Converter' support

原因:默認只能轉換BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、URL幾種類型,== 所以必須自己手動編寫一個轉換類==
我原本得實體類:用的數據類型是:LocalDateTime,不能自動轉換

修改后:自己編輯了一個轉換類LocalDateConverter
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ExcelProperty(value="時間",index = 10,converter = LocalDateConverter.class)
@ColumnWidth(25)
private LocalDateTime turnoutTime;LocalDateConverter
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;
public class LocalDateConverter 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 excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Override
public CellData convertToExcelData(LocalDateTime localDate, ExcelContentProperty excelContentProperty
, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData<>(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}一般情況下應該是解決了!?。?/p>
但是部署后問題還是一樣??!也不知道為什么沒有生效。
于是乎,經過不斷得嘗試,還是報一樣得錯,無奈之下我只能在:EasyExcel寫入數據之前得到list集合循環(huán)遍歷,然后逐個用字符串進行時間轉換
實體類更改:用一個String字符來代替LocalDateTime 來寫入Excel

代碼
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ExcelIgnore
// @ExcelProperty(value="時間",index = 10,converter = LocalDateConverter.class)
private LocalDateTime turnoutTime;
@ExcelProperty(value="時間",index = 10)
private String turnoutTimeString;邏輯層更改
List<Student> stus= (List<Student>) studentService.listByIds(student.getIds());
log.info("stuss數據條數:{}",stus.size());
List<Student> list = JSON.parseArray(JSON.toJSONString(stus), Student.class);
for (Student stu: list) {
//類型的判斷
String sex="男";
if (stu.getSex() == 1) {
sex="男";
} else if (stu.getSex() == 2) {
sex="女";
}
stu.setSexName(sex);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//將LocalDateTime轉換String
stu.setTurnoutTimeString(stu.getTurnoutTime().format(formatter));
}
excelWriter.write(list, sheet);
excelWriter.finish();最后到這里再進行運行,就沒什么問題了,只是可能有些性能的消耗,畢竟要是數據量大,10w+的數據,這種情況有待考量,至于轉換類不生效的問題后期發(fā)現(xiàn)了再更吧,有知道的伙計們也分享一下唄?。。ㄆ婀值氖俏以谧⒔饫锩婕拥胕ndex = 10得序號和 @ColumnWidth(25)都沒有生效,當然類型的轉換類也沒有生效!?。。?/p>
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合RestTemplate用法的實現(xiàn)
本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價值,感興趣的可以了解一下2023-08-08
Java大對象存儲之@Lob注解處理BLOB和CLOB數據的方法
本文將深入探討@Lob注解的使用方法、最佳實踐以及在處理大對象存儲時應當注意的性能與內存考量,我們將通過實際示例展示如何在Java應用中有效地管理和操作BLOB和CLOB數據,感興趣的朋友一起看看吧2025-05-05
SSH框架網上商城項目第21戰(zhàn)之詳解易寶支付的流程
這篇文章主要為大家詳細介紹了SSH框架網上商城項目第21戰(zhàn)之易寶支付的流程,感興趣的小伙伴們可以參考一下2016-06-06
idea 創(chuàng)建properties配置文件的步驟
這篇文章主要介紹了idea 創(chuàng)建properties配置文件的步驟,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

