Java读取Excel文件

时间:2016-05-31 17:10:00

标签: java excel

我相信我拥有我需要的所有jar文件,而且我仍然收到一条错误,上面写着:

  

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文件用于项目:

公地编解码器1.10.jar
公地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 个答案:

答案 0 :(得分:0)

如果查看StackTrace,您会发现异常是由以下原因引起的:

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

基于此我认为您需要在类路径中包含: xmlbeans-xmlpublic-2.6.0.jar ,尝试将此额外的jar添加到类路径中并再试一次...... < / p>

我建议使用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>