easyexcel中poi

陈旧的项目导出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中的类区分开。

  1. 重新编译easyexcel源码包,使用maven-shade-plugin替换poi包名

  2. 将打好的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>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 创建一个java maven工程 Step1 添加bytebuddy及日志依赖 Step2 书写Agent的入口处...
    hezhangjian阅读 1,590评论 0 2
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 实用技巧 Maven 提速 多线程 跳过测试 编译失败后,接着编译 跳过失败的模块,编译到最后再报错 使用 Nex...
    宇宙湾阅读 3,053评论 0 1
  • 类冲突问题解决方式就是将冲突的包shade掉(将类的全路径名改掉,避免跟blink的依赖冲突)。具体配置方式如下:...
    冰__羽阅读 373评论 0 0
  • spring-boot-starter-parent.xml完整代码如下: <?xml version="1.0"...
    Harry_3300阅读 368评论 0 0