【问题标题】:How to resolve the InvocationTargetException in Apache POI如何解决 Apache POI 中的 InvocationTargetException
【发布时间】:2017-11-29 11:07:37
【问题描述】:

我正在尝试读取 xlsx 格式的 excel 文件。但是我得到 Result 太大并且 Invalid handle exception 。我将文件实例放在 FileInputStream 对象中,并尝试在 XSSFWorkbook 中设置该对象。但我得到了这个例外。

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

我给出了我的代码 sn-p 和下面的异常还有我正在使用的 jar

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)

请帮忙。

【问题讨论】:

  • 我增加了堆空间,但出现以下异常
  • 有人对此有任何想法吗?请帮忙

标签: java excel apache apache-poi


【解决方案1】:

你可以试试centichere的解决方案,特别是带有POI的流API的部分

【讨论】:

  • 我增加了堆空间,但出现以下异常
  • 你试过串流了吗?
  • @a.oliver 我认为这是一个不同的问题,你能帮忙吗??
猜你喜欢
  • 2015-07-28
  • 2011-04-06
  • 2019-09-11
  • 2020-07-03
  • 2012-11-21
  • 2020-12-05
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
相关资源
最近更新 更多