陳舊的項(xiàng)目導(dǎo)出excel時(shí)用的POI3.14唐瀑,現(xiàn)在引入easyexcel2.2.6群凶,報(bào)錯(cuò)。
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之前用的地方很多,提升版本代價(jià)太大力穗。
現(xiàn)使用maven-shade-plugin來保證POI多版本共存毅弧。原理是將POI3.17中的類換一個(gè)包名,從而和POI3.14中的類區(qū)分開当窗。
重新編譯easyexcel源碼包够坐,使用maven-shade-plugin替換poi包名
-
將打好的easyexcel.jar放入項(xiàng)目中,添加到本地依賴
項(xiàng)目中也用到了asm3.3.1崖面,easyexcel引用了asm4.2元咙,也將asm包名替換
<!--easyexcel打包時(shí)的插件--> <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> <!-- 項(xiàng)目引用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>