如何解决Apache POI中的InvocationTargetException

时间:2017-11-29 11:07:37

标签: java excel apache apache-poi

我正在尝试读取xlsx格式的excel文件。但我得到的结果太大和无效的句柄异常。我将文件实例放在FileInputStream对象中,并尝试在XSSFWorkbook中设置该对象。但我得到了这个例外。

我在没有安装Microsoft Office并且有开放式办公室的环境中工作。我没有在开放式办公室中编辑我的xlsx文件。但是我在开放式办公室打开文件,然后尝试运行poi代码.Is这有什么问题吗?

我正在提供我的代码片段以及下面的例外我正在使用的罐子

commons-codec-1.10
commons-collections4-4.1
commons-io-2.5
commons-logging-1.2
curvesapi-1.04
ooxml-schemas-1.0
poi-3.17
poi-excelant-3.17
poi-ooxml-3.17
xmlbeans-2.3.0

这是我获得例外的代码,

 private void openWorkbook(String fileName) throws IOException {
    FileInputStream excelFile = new FileInputStream(new File(fileName));
    try {

        this.setWorkbook(new XSSFWorkbook(excelFile)); // here the exception is coccuring
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        excelFile.close();
    }
}

我相信setWorkBook方法无法处理工作簿的大小。

我得到的例外是

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:63)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:580)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:165)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:300)
at com.test.ExcelManager.openWorkbook(ExcelManager.java:26)
at com.test.ExcelManager.<init>(ExcelManager.java:18)
at com.test.ActivityLauncher.main(ActivityLauncher.java:87)
 Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:60)
... 6 more
 Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyle.getTableStyleElementList()Ljava/util/List;
at org.apache.poi.xssf.usermodel.XSSFTableStyle.<init>(XSSFTableStyle.java:87)
at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:247)
at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:141)
... 12 more
Exception in thread "main" java.lang.NullPointerException
at com.test.ExcelManager.<init>(ExcelManager.java:19)
at com.test.ActivityLauncher.main(ActivityLauncher.java:87)

请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以使用POI的流媒体API尝试中心here的解决方案

相关问题