EasyExcel:java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Cell.getCellType()情况

本文介绍如何解决在使用Apache POI旧版本时遇到的NoSuchMethodError错误,并通过整合easyExcel来兼容新特性,同时提供了解决LocalDateTime类型转换的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

确定,自身已解决java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Cell.getCellType()情况。

问题出现分析:

        公司本身老项目 poi依赖如下:修改依赖版本是不可能的  这辈子都不可能了

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>

解决:在不修改poi版本的前提下,只能让easyExcel想poi进行融合,导入2.1.6版本。借鉴的博客。

 easyExcel的maven依赖(备忘)_不开窍的博客-优快云博客_easyexcel依赖

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


引入后,解决找不到方法的问题。

但是因为2.1.6的版本 (貌似有点低),无法兼容 LocaldateTime类型,如果项目导出excel需要用到LocalDateTime字段 需要创建一个配置类。原文来自下面的博主:

Can not find ‘Converter‘ support class LocalDateTime_爱喝椰汁的木木的博客-优快云博客

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值