Search code examples
javaexceljxls

org/apache/poi/openxml4j/exceptions/InvalidFormatException using JXLS


I am using JXLS Java library to generate excel reports. I am trying to run the sample getting started tutorial (Object collection output demo). As suggested in the tutorial I am using maven to specify the required libraries in my project build configuration file. Below is the java code used

List<Employee> employees = generateSampleEmployeeData();
    try(InputStream is = ObjectCollectionDemo.class.getResourceAsStream("object_collection_template1.xls")) {
        try(OutputStream os = new FileOutputStream("target/" + fileName)) {
            Context context = new Context();
            context.putVar("employees", employees);
            JxlsHelper.getInstance().processTemplate(is, os, context);
        }
    }

When i run the program in my eclipse i get the below exception:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:34)
    at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:217)
    at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:104)
    at com.ucas.ObjectCollectionDemo.main(ObjectCollectionDemo.java:42)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.exceptions.InvalidFormatException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 9 more

Below is a snapshot of the maven dependencies enter image description here

Java Version: 1.8

Environment: Windows 7

IDE: Eclipse Neon


Solution

  • It looks like you have an issue with some XML dependencies required for Apache POI processing.

    I can see them in your dependencies screenshot however for some reason they are not in effect when you are running your program. May be there is some conflict between the dependencies.

    Try to create a minimal jxls project by just adding only the following two dependencies and see if it works

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

    Then you can add your own dependencies one by one and see at which point it breaks.