maven poi

在升级POI至3.10.1版本后,遇到启动时的ClassCastException异常,进一步升级到3.12版本,虽然解决了启动问题,但在执行导出操作时出现NoClassDefFoundError,原因是POI与JDK中的stax-api jar包冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前的项目中,poi版本过低,不支持excel2007的导入导出,遂升级至3.10.1,项目启动的时候却报

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory

研究后发现是jar冲突的缘故,在pom中去除对sml-apis的依赖即可。

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
            <exclusions>
          <exclusion>
              <groupId>xml-apis</groupId>
              <artifactId>xml-apis</artifactId>
          </exclusion>
         </exclusions>
     </dependency>


poi 3.12版本修复了这个bug,却引出了另外一个bug,项目能正常启动,但是在执行导出操作的时候抛出
java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller
实际上是 xxx can not cast to javax.xml.streaming.EventFactory造成的。
原因是3.10.1以后,stax-api与jdk1.6.18jar冲突。


            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.12</version>
            <exclusions>
    <exclusion>
              <groupId>stax</groupId>
              <artifactId>stax-api</artifactId>
          </exclusion>
         </exclusions>
     </dependency>



<pre name="code" class="html">要保证项目编译的时候要编译版本要高于6U18。

 
<span style="font-family: Arial, Helvetica, sans-serif;"><build>  </span>
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration>  
        </plugin>  
    </plugins>  
</build>



                
通常业务需求都是客户端一个导出按钮,发送请求到服务端,服务端写一个接口导出报表到客户端,客户可以自行下载。无论Struts或者springMVC均可。 @RequestMapping("Export") @ResponseBody public String getAll(HttpServletRequest request,HttpServletResponse response) throws IOException{ //集合为需要导出数据,数据查询得到,这里测试就不写了。 List<User> list=new ArrayList<User>(); // 生成Excel文件 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet sheet = hssfWorkbook.createSheet("测试数据"); // 表头 HSSFRow headRow = sheet.createRow(0); headRow.createCell(0).setCellValue("姓名"); headRow.createCell(1).setCellValue("手机号码"); headRow.createCell(2).setCellValue("年龄"); // 表格数据 for (User user : list) { HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1); dataRow.createCell(0).setCellValue(user.getName()); dataRow.createCell(1).setCellValue(user.getPhone()); dataRow.createCell(2).setCellValue(user.getAge()); } // 下载导出(一个流两个头) // 设置头信息 response.setContentType( "application/vnd.ms-excel"); // MIME .jpg .xls .mp3 .avi .txt .exe String filename = "驾驶员数据.xls"; //如果为Struts框架,获得request和response可用ServletActionContext String agent = request .getHeader("user-agent"); filename = FileUtils.encodeDownloadFilename(filename, agent); response.setHeader("Content-Disposition", "attachment;filename=" + filename); ServletOutputStream outputStream = response .getOutputStream(); //输出 hssfWorkbook.write(outputStream); // 关闭 hssfWorkbook.close(); //System.out.println("导出成功"); return "success"; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值