我可以一起使用 jxls 和 apache poi 吗? [英] May I use jxls and apache poi together?

查看:56
本文介绍了我可以一起使用 jxls 和 apache poi 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序来分析一些数据,结果必须以 excel 文件的形式呈现.从这个意义上说,我开始使用 Apache POI (3.11).由于一些报告需要大量的时间和内存来重现,我进行了调查并找到了jxls,经过一些测试我认为是解决方案.但是现在我发现了一个问题:不能同时使用两个框架.

I'm making an application to analize some data and the result must be presented in excel files. In that sense I started to use Apache POI (3.11). Due to some reports consumes a lot of time and memory to be reproduce, I made an investigation and I found jxls, after some test I thought was the solution. But now I found a problem: can´t work both frameworks together.

  1. 我必须将 Apache POI 从 3.11 更新到 3.14,才能使用 jxls-2.3.0
  2. 我制作了一个额外的包,以便使用 jxls 进行测试,没问题
  3. 我尝试将我的一个类从 Apache POI 迁移到 jxls,但我收到此错误:java.lang.IllegalStateException:无法加载 XLS 转换器.请确保 Transformer 实现在类路径中.这是我的方法的代码:

  1. I have to update Apache POI from 3.11 to 3.14, in order to work with jxls-2.3.0
  2. I made an extra package in order to make my tests with jxls, not problem
  3. I try to migrated one of my classes from Apache POI to jxls, and a I got this error: java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath. This is the code of my method:

private void prepareNewReport(File excelFile) {
    List perforaciones = makePerforacionReport
                                            .makePerforacionData(escenario);

try (InputStream is =  ReportePerforacionTotalDialog.class
                .getResourceAsStream("PerforacionTotal_template.xls")){
    try (OutputStream os = new FileOutputStream(excelFile)) {
        Context context = new Context();
        context.putVar("perforaciones", perforaciones);
        JxlsHelper.getInstance().processTemplate(is, os, context);
        LOGGER.logger.log(Level.INFO, "Archivo de perfortacion generado con éxito");
    }
} catch (IOException e) {
    LOGGER.logger.log(Level.SEVERE, "Problemas buscando el archivo", e);
}

}

这怎么可能?在同一个项目中,我有我的测试类,只是另一个包并且它工作正常.正如您所看到的,它与 jxls 页面中的示例并没有太大的不同,并且导入是相同的.

How could be this possible?. In the same project I have my test class, just another package and its working fine. As you can see it´s not so much different from the example in the jxls page and the imports are the same.

但更糟糕的是,当我试图使清洁 &构建我的项目,然后我遇到了另一个错误:

But even worst, when I tried to make clean & build of my project, then I got this other error:

java.lang.RuntimeException: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray not found

为了与 jxls 和 apache poi 一起工作,我查看了我导入的每个库,这是正确的,那个类不存在.只是为了看看这两个框架之间是否存在冲突,我从类路径中删除了使用 jxls 所需的所有库.清洁&再次构建,没有问题,我有我的 .jar 文件要发送给我的客户,但不完整.

I looked at every library that I importede in order to work with jxls and apache poi, and that´s rigth, that class is not there. Just to see if there a conflict among these two framewoks, I eliminated from the class path all libraries needed to use jxls. Clean & build again, and not problem, I have my .jar file to send to my customer, but incomplete.

我可以尝试替换所有使用 Apache POI 的类,但这意味着很多工作,因为在我的项目中使用 POI 多次读取带有数据的 excel 文件并将另外许多文件写入 excel.我计划使用 jxls 以利用使用模板.

I could try to replace all classes that use Apache POI, but that means a lot of work, since POI is used in my project to read excel files with data many times and to write another many files to excel. I planned to use jxls in order to take advantage of use templates.

我会感谢任何帮助或建议.

I will apreciate any help or suggestion.

推荐答案

对于第一个错误,运行应用程序时,您的类路径中似乎缺少 Apache POI 的 JXLS 转换器.在此处查看 JXLS 入门信息:http://jxls.sourceforge.net/getting_started.html

For the first error, it would appear that the JXLS transformer for Apache POI is missing in your classpath when running the application. Check the JXLS getting started info here: http://jxls.sourceforge.net/getting_started.html

正如在 Transformers 部分(见主要概念)中所解释的那样)Jxls 核心模块不依赖于任何特定的 Java-Excel 库,并且只通过预定义的接口与 Excel 配合使用.目前,Jxls 在基于众所周知的 Apache POI 和 Java Excel API 库的单独模块中提供了此接口的两种实现.

As it is explained in Transformers section (see Main Concepts)) Jxls core module does not depend on any specific Java-Excel library and works with Excel exclusively through a predefined interface. Currently Jxls supplies two implementations of this interface in separate modules based on the well-known Apache POI and Java Excel API libraries.

如果您使用的是 maven,请确保在您的 pom.xml 中包含 JXLS 入门页面上列出的 jxls-poi 依赖项:

If you're using maven, be sure to include in your pom.xml the jxls-poi dependency listed on the JXLS getting started page:

<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls-poi</artifactId>
    <version>1.0.9</version>
</dependency>

对于第二个问题,org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray 不在 apache POI ooxml schemas jar 文件中,用于 3.11 (poi-ooxml-schemas-3.11-20141221.jar) 或 3.14 (poi-ooxml-schemas-3.14-20160307.jar).POI 使用一组精简的 ooxml 模式类,您需要从 http://central.maven.org/maven2/org/apache/poi/ooxml-schemas/1.3/ 或者如果您使用的是 maven(或其他构建工具),从 https://mvnrepository.com/artifact/获取构建的依赖项org.apache.poi/ooxml-schemas/1.3

For the second issue, org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray is not in the apache POI ooxml schemas jar files for either 3.11 (poi-ooxml-schemas-3.11-20141221.jar) or 3.14 (poi-ooxml-schemas-3.14-20160307.jar). POI uses a stripped down set of ooxml schema classes, you will need to get the ooxml schemas complete jar from http://central.maven.org/maven2/org/apache/poi/ooxml-schemas/1.3/ or if you're using maven (or another build tool), get the dependency for your build from https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3

例如对于 maven:

e.g for maven:

<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>

请务必从您的 maven pom.xml 中删除 poi-ooxml-schemas 依赖项,以便上面的 ooxml-schemas 优先.

Be sure to remove the poi-ooxml-schemas dependency from your maven pom.xml so that ooxml-schemas above takes precedence instead.

这篇关于我可以一起使用 jxls 和 apache poi 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆