简体   繁体   English

Apache POI工作簿引发java.lang.reflect.InvocationTargetException

[英]Apache POI Workbook throws java.lang.reflect.InvocationTargetException

I am trying to instantiate a Workbook with this code: 我正在尝试使用以下代码实例化Workbook

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

I do it this way because the workbook can either be .xlsx or .xls . 我这样做是因为工作簿可以是.xlsx.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: 我进行了一些研究,发现InvocationTargetException不是真正的异常,因此我尝试使用以下catch语句捕获它:

} 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. 但是,我在Eclipse中遇到一个错误,说我无法捕捉到它,因为它从不抛出任何东西。 Why am I getting this error? 为什么会出现此错误? Any help would be appreciated, thanks in advance! 任何帮助将不胜感激,在此先感谢!

Note: I am using Apache POI 3.14. 注意:我正在使用Apache POI 3.14。

The actual error getting thrown is NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions; 引发的实际错误是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 可能您的类路径中没有xmlbean jar,或者您可能有多个版本的jar

You can instantiate the workbook separately for .xls and .xlsx extension based on the file name: 您可以根据文件名分别实例化.xls和.xlsx扩展名的工作簿:

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. 我已经用相同版本更新了pom。 Earlier I have different version 3.9.0 for poi-ooxml and 4.0 for poi 早些时候,我对poi-ooxml使用了不同的版本3.9.0,对于poi有4.0

<!-- 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 对我有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ApachePOI生成org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException - ApachePOI generates org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException - java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException? - java.lang.reflect.InvocationTargetException? JavaFX MediaPlayer抛出java.lang.reflect.InvocationTargetException - JavaFX MediaPlayer throws java.lang.reflect.InvocationTargetException 签名的jar引发java.lang.reflect.InvocationTargetException - Signed jar throws java.lang.reflect.InvocationTargetException 下面的代码引发java.lang.reflect.InvocationTargetException - Code below Throws java.lang.reflect.InvocationTargetException org.apache.poi.POIXMLException:使用Libre Office更新.xls文件后POI API中的java.lang.reflect.InvocationTargetException - org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException in POI API after an .xls file is updated with Libre Office org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException,根本原因是空指针异常(xmlbeans.jar) - org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException,root cause nullpointer exception(xmlbeans.jar) Java中的java.lang.reflect.InvocationTargetException - java.lang.reflect.InvocationTargetException in Java 播放框架:[RuntimeException:java.lang.reflect.InvocationTargetException] - Playframework: [RuntimeException: java.lang.reflect.InvocationTargetException]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM