2016-05-31 128 views
0

我相信我有我需要的所有jar文件,我仍然收到這個說由導致錯誤:的Java讀取Excel文件

java.lang.reflect.InvocationTargetException

原諒我,我在閱讀錯誤信息方面仍然不太好。該錯誤提示的問題是與線"Workbook book = new XSSFWorkbook(stream);"

import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.IOException; 
    import java.util.Iterator; 

    import org.apache.poi.ss.usermodel.Cell; 
    import org.apache.poi.ss.usermodel.Row; 
    import org.apache.poi.ss.usermodel.Sheet; 
    import org.apache.poi.ss.usermodel.Workbook; 
    import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

    public class excelReader { 
public static void main(String[] args) throws IOException{ 

    String spreadSheetFilePath = "C:\\Users\\userExample\\Documents\\forJava.xlsx"; 
    FileInputStream stream = new FileInputStream(new File(spreadSheetFilePath)); 

    Workbook book = new XSSFWorkbook(stream); 
    Sheet sheetOne = book.getSheetAt(0); 
    Iterator<Row> iterator = sheetOne.iterator(); 

    while(iterator.hasNext()) { 
     Row nextRow = iterator.next(); 
     Iterator<Cell> cellIter = nextRow.cellIterator(); 

     while(cellIter.hasNext()) { 
     Cell cell = cellIter.next(); 

     switch (cell.getCellType()) { 
     case Cell.CELL_TYPE_STRING: 
      System.out.print(cell.getStringCellValue()); 
      break; 
     case Cell.CELL_TYPE_BOOLEAN: 
      System.out.print(cell.getBooleanCellValue()); 
      break; 
     case Cell.CELL_TYPE_NUMERIC: 
      System.out.print(cell.getNumericCellValue()); 
      break; 
     } 
     System.out.print(" - "); 
     } 
     System.out.println(); 
    } 
    book.close(); 
    stream.close(); 
} 

}

錯誤我得到

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:279) 
at excelReader.main(excelReader.java:18) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
at java.lang.reflect.Constructor.newInstance(Unknown Source) 
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56) 
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62) 
... 4 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) 
... 10 more 

Jar文件在我的圖書館項目:

commons- codec-1.10.jar
commons-logging1-2.jar
的dom4j-1.1.jar
的JUnit 4.1.2.17.jar
POI-3.14-20160307.jar
POI-excelant-3.14-20160307.jar
POI-OOXML-架構 - 3.14-20160307.jar
POI暫存器-3.14-20160307.jar
的xmlbeans-2.3.0.jar

+1

...張力升高:-)什麼** **是實際跟蹤/錯誤信息?也許這裏有人擅長閱讀它...... – Dilettant

+0

問題區域更適合追溯。允許更多字符和行保持行 – Dilettant

+0

歡迎您,不需要找藉口。應該有一個編輯鏈接接近你的問題。評論還有其他問題,您只能在創建後不超過五分鐘才能對其進行編輯。 – Dilettant

回答

0

如果你看一下堆棧跟蹤,你可以發現異常的原因是:

java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions; 

基於該我認爲你需要的:的xmlbeans-xmlpublic-2.6.0.jar類路徑中,嘗試這種額外的jar添加到類路徑,然後再試一次......

我建議管理使用Maven的依賴,這將簡化一切,只需切換到Maven和添加以下行到你的POM文件:

<dependency> 
    <groupId>org.apache.poi</groupId> 
    <artifactId>poi</artifactId> 
    <version>3.14</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.poi</groupId> 
    <artifactId>poi-ooxml</artifactId> 
    <version>3.14</version> 
</dependency> 
+0

我用那個jar文件更新了類路徑,但沒有解決問題。 – Sam

+0

將xmlbeans-xmlpublic更新到2.6.0 – aleroot

+0

這是最後一個jar文件「將xmlbeans-xmlpublic更新爲2.6.0」 - aleroot。感謝大家的幫助 – Sam