陈旧的项目导出excel时用的POI3.14,现在引入easyexcel2.2.6,报错。
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/util/DefaultTempFileCreationStrategy
at com.alibaba.excel.util.FileUtils.createPoiFilesDirectory(FileUtils.java:138)
at com.alibaba.excel.write.ExcelBuilderImpl.<clinit>(ExcelBuilderImpl.java:30)
at com.alibaba.excel.ExcelWriter.<init>(ExcelWriter.java:47)
at com.alibaba.excel.write.builder.ExcelWriterBuilder.build(ExcelWriterBuilder.java:130)
at com.alibaba.excel.write.builder.ExcelWriterBuilder.sheet(ExcelWriterBuilder.java:146)
at com.alibaba.excel.write.builder.ExcelWriterBuilder.sheet(ExcelWriterBuilder.java:134)
easyexcel依赖POI3.17+,POI版本冲突。POI3.14之前用的地方很多,提升版本代价太大。
现使用maven-shade-plugin来保证POI多版本共存。原理是将POI3.17中的类换一个包名,从而和POI3.14中的类区分开。
重新编译easyexcel源码包,使用maven-shade-plugin替换poi包名
-
将打好的easyexcel.jar放入项目中,添加到本地依赖
项目中也用到了asm3.3.1,easyexcel引用了asm4.2,也将asm包名替换
<!--easyexcel打包时的插件--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>true</createDependencyReducedPom> <relocations> <relocation> <!-- poi包替换 --> <pattern>org.apache.poi</pattern> <shadedPattern>shade.org.apache.poi</shadedPattern> </relocation> <relocation> <pattern>org.objectweb.asm</pattern> <shadedPattern>shade.org.objectweb.asm</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> <!-- 项目引用easyexcel --> <!-- poi3.14和poi3.17共存,asm3.3.1和asm4.2共存,重新打包easyexcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/easyexcel-2.2.6.jar</systemPath> <scope>system</scope> </dependency>