easyExcel与poi版本不兼容导致的后台报错问题

1、背景:最新接手公司系统excel导入解析模块,点击批量导入,后台报错如下

com.alibaba.excel.exception.ExcelAnalysisException: java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/FileMagic
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:57)
    at com.alibaba.excel.ExcelReader.<init>(ExcelReader.java:145)
    at com.alibaba.excel.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:193)
    at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:229)
    at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:217)
    at test.jifen.ZJpsbcTest.parseExcel(ZJpsbcTest.java:267)
    at test.jifen.ZJpsbcTest.test1(ZJpsbcTest.java:190)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/FileMagic
    at com.alibaba.excel.support.ExcelTypeEnum.recognitionExcelType(ExcelTypeEnum.java:90)
    at com.alibaba.excel.support.ExcelTypeEnum.valueOf(ExcelTypeEnum.java:78)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.choiceExcelExecutor(ExcelAnalyserImpl.java:62)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:51)
    ... 29 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.poifs.filesystem.FileMagic
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 33 more

2、问题:easyExcel与poi版本不一致问题(easyExcel中包含poi)

项目中引入的easyExcel与poi版本如下:

<org.apache.poi-version>3.15-beta1</org.apache.poi-version>
<com.alibaba.easyexcel-version>3.0.1</com.alibaba.easyexcel-version>
ps:easyExcel 3.0.1版本中包含poi 的4.1.2版本

3、解决:解决版本一般是兼容高版本,所以将poi的版本统一修改为4.1.2版本;并且三个poi:poi,poi-ooxml,poi-ooxml-schemas的版本号要一致。

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

ps:升级poi的过程中可能需要修改接口,比如Cell.CELL_TYPE_STRING升级后替换为STRING等。

4、上面的bug解决后又出现新bug,如下

Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;

5、原因:查看类org.apache.xmlbeans.XmlOptions,搜索发现这个类确实没有setEntityExpansionLimit()这个方法;
easyExcel与poi版本不兼容导致的后台报错问题_第1张图片

6、解决方案:升级xmlbeans的版本如下

		<dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>
        ===============修改为3.1.0版本===========
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>

ps:再次查看idea中左侧的lib目录,现在xmlbeans的jar已经更新为3.1.0版本,并且有了setEntityExpansionLimit方法,至此依赖冲突问题解决。

总结:
1、解决依赖冲突一般需要升级处理
2、可以通过idea左侧的libraries目录查看导入jar的版本信息以及java类
3、可以通过idea插件Maven Helper的形式查看maven依赖冲突

引用地址

你可能感兴趣的:(大数据,poi,junit,java,单元测试)