使用easyPOI时碰到的几个问题(记录一下)

文章目录

    • 前言
      • 环境
      • 问题1(ArrayListValuedHashMap找不到)
      • 问题2(CellType类找不到)
      • 问题3(CellStyle.setAlignment)

前言

前面我们说了如何使用easyPOI,详情请参考全网最全最简单使用easypoi导入导出Excel的操作手册,今天我来记录下在使用easyPOI时碰到的几个问题,本来以为上传下载功能使用EasyPOI之后挺简单的,结果翻车了,一个上传和下载就因为版本的问题搞了老半天。真的是很愁人呀。下面就是我系统的初始环境。

环境

    <!-- springboot -->
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
	 <!-- springboot -->
	 
	<!--easypoi-->
	 <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.0.0</version>
        </dependency>
 <!--easypoi-->
 
 <!--commons-collections4-->
 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>
	 <!--commons-collections4-->

本来以为一件非常简单的事情,结果一连碰到了三个问题,啪啪打脸呀。问题分别如下:

问题1(ArrayListValuedHashMap找不到)

提示ArrayListValuedHashMap找不到的问题,报错如下图所示:
使用easyPOI时碰到的几个问题(记录一下)_第1张图片
这个问题主要就是因为commons-collections4的版本是4.0,版本比较低,升级到4.1后就可以了。,升级后的依赖如下:

     <dependency>
           <groupId>org.apache.commonsgroupId>
           <artifactId>commons-collections4artifactId>
           <version>4.1version>
       dependency>

解决第一个问题之后,紧接着我就碰到第二个问题。上传的时候文件的时候同样还是提示某某类找不到的问题。这有点让人揪心。

问题2(CellType类找不到)

第二个问题如下图所示:提示CellType类找不到,真的就很奇怪了,这个类明明可以搜索的到,为啥系统就提示找不到呢?
使用easyPOI时碰到的几个问题(记录一下)_第2张图片
一番百度之后还是无果,莫得办法,我只有尝试着将easypoi-spring-boot-starter依赖的版本从4.0.0降到3.3.0。版本换成3.3.0之后,上传文件就可以了,真的是太神奇了。

  <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-spring-boot-starterartifactId>
            <version>3.3.0version>
        dependency>  

问题3(CellStyle.setAlignment)

搞定上传文件的功能之后,我很开行,紧接着就去试试下载文件的功能了。哎,老天捉弄我呀,之前一直好好的的下载功能,就在我将easypoi-spring-boot-starter依赖的版本从4.0.0降到3.3.0之后,就不行了。报错如下:

Caused by: java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.CellStyle.setAlignment(S)V
	at cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl.stringNoneStyle(ExcelExportStylerDefaultImpl.java:69) ~[easypoi-base-3.3.0.jar:?]
	at com.nuonuo.juhe.common.util.excel.ExcelStyleUtil.stringNoneStyle(ExcelStyleUtil.java:42) ~[classes/:?]
	at cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler.createStyles(AbstractExcelExportStyler.java:44) ~[easypoi-base-3.3.0.jar:?]
	at cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl.(ExcelExportStylerDefaultImpl.java:31) ~[easypoi-base-3.3.0.jar:?]
	at com.nuonuo.juhe.common.util.excel.ExcelStyleUtil.(ExcelStyleUtil.java:16) ~[classes/:?]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_181]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_181]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_181]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_181]
	at cn.afterturn.easypoi.excel.export.ExcelExportService.insertDataToSheet(ExcelExportService.java:225) ~[easypoi-base-3.3.0.jar:?]

哎,又是这个CellStyle类的问题,真的很愁人呀。没有办法,只得继续去处理。又是一番百度。从网上得到了一个答案,说的是需要将poi-ooxml的版本从3.17降到3.15。但是,我找了一圈我也没找到项目中有直接引入poi-ooxml这个依赖。找了半天发现项目中一个工具类中引入了poi-ooxml依赖,版本号是3.17。(PS: 找依赖的方法是:在Maven Projects中选中项目,右键Show Dependencies)而easypoi-spring-boot-starter依赖已经引入的poi-ooxml依赖的版本是3.15。 这里就出现了版本冲突。处理方式就是将工具类中的那个poi-ooxml依赖去除掉。

 <exclusions>
                <exclusion>
                    <groupId>org.apache.poigroupId>
                    <artifactId>poi-ooxmlartifactId>
                exclusion>
            exclusions>

这三个问题处理好之后,上传文件和下载文件就处理好了。

你可能感兴趣的:(个人总结)