简体   繁体   中英

Apache POI Workbook throws java.lang.reflect.InvocationTargetException

I am trying to instantiate a Workbook with this code:

Workbook workbook = WorkbookFactory.create(new File(excelWorkbookFilePath));

I do it this way because the workbook can either be .xlsx or .xls . However, it ends up throwing this error:

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:249)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
    at com.iconectiv.handle.ExcelHandle.<init>(ExcelHandle.java:43)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
    ... 9 more
Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    at org.apache.poi.POIXMLTypeLoader.<clinit>(POIXMLTypeLoader.java:50)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)
    ... 15 more

I did some research and found out that InvocationTargetException is not the real exception, so I tried to catch it with this catch statement:

} catch (InvocationTargetException e) {
    e.getCause().printStackTrace();
}

However, I get an error in Eclipse saying that I can't catch that as it is never thrown from anything. Why am I getting this error? Any help would be appreciated, thanks in advance!

Note: I am using Apache POI 3.14.

The actual error getting thrown is NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;

Possibility you don't have xmlbean jar in your class path or you might have multiple version of jars

You can instantiate the workbook separately for .xls and .xlsx extension based on the file name:

Workbook wb = null;
    if (fileName.endsWith(".xlsx")) {
        wb = new XSSFWorkbook(in);
    }else{
        fs = new POIFSFileSystem(in);
        wb = (Workbook) new HSSFWorkbook(fs);   
    } 

I have updated the pom with same version. Earlier I have different version 3.9.0 for poi-ooxml and 4.0 for poi

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

It worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM