May I use jxls and apache poi together?

2019-07-20 13:42发布

问题:

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. 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);
    }
    
    }

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

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.

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.

回答1:

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

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.

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>

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

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>

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