欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何解決EasyExcel導(dǎo)出文件LocalDateTime報錯問題

 更新時間:2023年06月09日 09:43:30   作者:陳寶子  
這篇文章主要介紹了如何解決EasyExcel導(dǎo)出文件LocalDateTime報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

解決EasyExcel導(dǎo)出文件LocalDateTime報錯

問題引出

我在參與一個項目的時候接觸到數(shù)據(jù)表格導(dǎo)出為Excel表格的需求,但是在導(dǎo)出的時候會出現(xiàn)報錯

Cannot find ‘Converter’ support class LocalDateTime

原因是我需要導(dǎo)出的實體類中存在 LocalDateTime 類型的屬性,而又恰巧 EasyExcel 不支持 LocalDate 和 LocalDateTime 接收數(shù)據(jù),啊人生

解決方案

在尋找了多篇文章之后,在下面對這個問題進行總結(jié),既然默認不支持我使用這個類型,那就殺出一條路來。

首先先來看報的錯誤提示:

Cannot find ‘Converter’ support class LocalDateTime

明顯可以看出來是找不到 LocalDateTime 的一個 Converter,那么這個所謂的 Converter 到底是個什么東西呢?既然他缺少這個東西我們能不能給他自己弄一個上去呢?說干就干!(其實解決方法也真就是這個)

自定義Converter

Converter 在這里其實是一個字段轉(zhuǎn)換器,在 EasyExcel 中擔(dān)任將Java屬性轉(zhuǎn)換成Excel表格中合法數(shù)據(jù)的一個小東西。

由于EasyExcel自己沒有我們需要的 LocalDateTime 的字段轉(zhuǎn)換器,那我們就自己搞一個出來。

在 config 包下新建一個自定義的字段轉(zhuǎn)換器 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轉(zhuǎn)換器
 * @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

既然這東西是本來沒有然后我們自己弄出來的,那肯定要告訴程序:**誒!這里我給你生了個崽,要記得領(lǐng)回去啊!**所以,我們在原本需要導(dǎo)出的 LocalDateTime 類型中的 @ExcelProperty 注解中引入自定義的字段轉(zhuǎn)換器。

同時要加上一個JsonFormat的格式化注解,將該屬性轉(zhuǎn)換成 json格式,這里要注意的就是要導(dǎo)入一下阿里巴巴的 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>

好啦!到這里我們遇到的問題就已經(jīng)解決了呀!雖然說前期剛遇到這個問題的時候一直在被折磨,但是卻是一個很好的一個提升機會,沒點兒bug,怎么能保得住頭發(fā)呢是吧。

EasyExcel導(dǎo)出問題

報錯信息!??!

錯誤一:

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

上面兩種報錯!?。《际且粋€原因,各種查詢之后,說是版本沖突,這種就很頭疼

經(jīng)過排查,我在pom中將單元測試的依賴注解之后,導(dǎo)入就正常了

錯誤二:

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

原因:默認只能轉(zhuǎn)換BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、URL幾種類型,== 所以必須自己手動編寫一個轉(zhuǎn)換類==

我原本得實體類:用的數(shù)據(jù)類型是:LocalDateTime,不能自動轉(zhuǎn)換

修改后:自己編輯了一個轉(zhuǎn)換類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")));
    }
}

一般情況下應(yīng)該是解決了?。。?/p>

但是部署后問題還是一樣?。∫膊恢罏槭裁礇]有生效。

于是乎,經(jīng)過不斷得嘗試,還是報一樣得錯,無奈之下我只能在:EasyExcel寫入數(shù)據(jù)之前得到list集合循環(huán)遍歷,然后逐個用字符串進行時間轉(zhuǎ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數(shù)據(jù)條數(shù):{}",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轉(zhuǎn)換String
                    stu.setTurnoutTimeString(stu.getTurnoutTime().format(formatter));
                }
                excelWriter.write(list, sheet);
           		excelWriter.finish();

最后到這里再進行運行,就沒什么問題了,只是可能有些性能的消耗,畢竟要是數(shù)據(jù)量大,10w+的數(shù)據(jù),這種情況有待考量,至于轉(zhuǎn)換類不生效的問題后期發(fā)現(xiàn)了再更吧,有知道的伙計們也分享一下唄?。。ㄆ婀值氖俏以谧⒔饫锩婕拥胕ndex = 10得序號和 @ColumnWidth(25)都沒有生效,當然類型的轉(zhuǎn)換類也沒有生效?。。。?/p>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java使用substring()截取(提取)子字符串

    Java使用substring()截取(提取)子字符串

    這篇文章主要介紹了Java使用substring()截取(提取)子字符串,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java中scheduleAtFixedRate的用法

    Java中scheduleAtFixedRate的用法

    如何正確使用Java中的scheduleAtFixedRate是一篇介紹Java中定時任務(wù)調(diào)度器的文章。它詳細介紹了scheduleAtFixedRate方法的用法、參數(shù)及作用,并給出了一些實例以幫助讀者更好地理解其使用。本文為Java開發(fā)人員提供了一些實用的技巧,幫助他們更好地管理和控制定時任務(wù)
    2023-04-04
  • 使用SpringBoot生成war包的流程步驟

    使用SpringBoot生成war包的流程步驟

    一般情況下,在SpringBoot項目開發(fā)完成進行服務(wù)器部署時,都是打成JAR包進行部署運行的,但是在有些情況下也需要將其打成War包使用Tomcat進行部署,本篇文章就簡單介紹一下SpringBoot如何打成War包,需要的朋友可以參考下
    2024-10-10
  • SpringBoot集成Milvus實現(xiàn)數(shù)據(jù)增刪改查功能

    SpringBoot集成Milvus實現(xiàn)數(shù)據(jù)增刪改查功能

    milvus支持的語言比較多,支持python, Java, Go,node等開發(fā)語言,本文主要介紹如何使用Java語言,采用springboot框架集成和調(diào)用Milvus數(shù)據(jù)庫,這篇文章主要介紹了SpringBoot集成Milvus,實現(xiàn)數(shù)據(jù)增刪改查,需要的朋友可以參考下
    2025-04-04
  • SpringBoot使用Sa-Token實現(xiàn)路徑攔截和特定接口放行

    SpringBoot使用Sa-Token實現(xiàn)路徑攔截和特定接口放行

    這篇文章主要介紹了SpringBoot使用Sa-Token實現(xiàn)路徑攔截和特定接口放行,文中通過代碼示例講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • JavaWeb實現(xiàn)顯示mysql數(shù)據(jù)庫數(shù)據(jù)

    JavaWeb實現(xiàn)顯示mysql數(shù)據(jù)庫數(shù)據(jù)

    MySQL是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在WEB應(yīng)用方面MySQL是最好的。本文將利用JavaWeb實現(xiàn)顯示mysql數(shù)據(jù)庫數(shù)據(jù)功能,需要的可以參考一下
    2022-03-03
  • SpringBoot 集成 activiti的示例代碼

    SpringBoot 集成 activiti的示例代碼

    這篇文章主要介紹了SpringBoot 集成 activiti的示例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 永久解決 Intellij idea 報錯:Error :java 不支持發(fā)行版本5的問題

    永久解決 Intellij idea 報錯:Error :java 不支持發(fā)行版本5的問題

    這篇文章主要介紹了永久解決 Intellij idea 報錯:Error :java 不支持發(fā)行版本5的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Spring中@PropertySource和@Value注解詳解

    Spring中@PropertySource和@Value注解詳解

    這篇文章主要介紹了Spring中@PropertySource和@Value注解詳解,@PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下
    2023-11-11
  • Java設(shè)計模式之原型模式詳細解讀

    Java設(shè)計模式之原型模式詳細解讀

    這篇文章主要介紹了Java設(shè)計模式之原型模式詳細解讀,原型模式屬于創(chuàng)建型設(shè)計模式,用于創(chuàng)建重復(fù)的對象,且同時又保證了性能,該設(shè)計模式的好處是將對象的創(chuàng)建與調(diào)用方分離,需要的朋友可以參考下
    2023-12-12

最新評論