使用Apache POI读取.xlsx文件会产生InvocationTargetException

时间:2017-02-15 05:44:23

标签: java apache-poi xlsx

我正在尝试使用Apache POI读取.xlsx文件,但遇到了错误:

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException

完整堆栈错误说明:

    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:625)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:637)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:186)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:290)
    at com.gemini.util.WorkbookObject.createExcelObj(WorkbookObject.java:77)
    at com.gemini.util.ObjectPool.prepareList(ObjectPool.java:24)
    at com.gemini.util.WorkbookObject.getInstance(WorkbookObject.java:36)
    at dhfl_idecimal.decimal_dhfl.calculator.Calculator.processWealthPlusAce(Calculator.java:33)
    at dhfl_idecimal.decimal_dhfl.calculator.Calculator.main(Calculator.java:167)
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)
    ... 9 more
Caused by: org.apache.xmlbeans.XmlException: Attribute "type" was already specified for element "v:shape".
    at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.read(XSSFVMLDrawing.java:138)
    at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.<init>(XSSFVMLDrawing.java:121)
    ... 15 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 549; Attribute "type" was already specified for element "v:shape".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at org.apache.poi.util.DocumentHelper.readDocument(DocumentHelper.java:137)
    at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.read(XSSFVMLDrawing.java:136)
    ... 16 more
java.lang.NullPointerException
    at dhfl_idecimal.decimal_dhfl.calculator.util.UpdateInput.updateInputParamForWealthPlusAce(UpdateInput.java:23)
    at dhfl_idecimal.decimal_dhfl.calculator.Calculator.processWealthPlusAce(Calculator.java:38)
    at dhfl_idecimal.decimal_dhfl.calculator.Calculator.main(Calculator.java:167)

PS:我提到了thisthis个问题,但没有帮助。

我也尝试添加org.apache.xmlbeans jar,但没有运气。

1 个答案:

答案 0 :(得分:1)

找到答案。不是一个确切的解决方案但有效
我的代码没有任何问题,因为我尝试阅读其他excels并且它完美无缺。问题在于excel。
可能的原因: 首先将excel解析为XML,当XML无效时,您会收到此错误。我的excel主要是使用MS-Office(Windows环境)开发的,我使用Libra Calc(在Linux环境中)编辑它,所以这可能是poi无法正确读取excel的原因。 解决方案:刚刚在Linux环境中开发了相同的excel,因为它运行良好。