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

查看:843
本文介绍了我可以一起使用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: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath.这是我的方法的代码:

  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

正如变形金刚"部分所述(请参阅主要概念"),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不在3.11(poi-ooxml-schemas-3.11-20141221.jar)或3.14(poi-ooxml-schemas-3.14-20160307)的apache POI ooxml架构jar文件中.罐). POI使用一组简化的ooxml模式类,您需要从 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:

<!-- 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天全站免登陆