编程语言
首页 > 编程语言> > 在java中读取Excel工作表时出现NoSuchFieldError

在java中读取Excel工作表时出现NoSuchFieldError

作者:互联网

我遵循了使用Apache POI XSSF构建工作簿的简单指南.按照相同的指南,我能够写一个Excel工作表,但是当试图从一个读取时,我收到代码后显示的错误.

码:

try {
    FileInputStream file = new FileInputStream(new File("howtodoinjava_demo.xlsx"));

    // Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(file);

    // Get first/desired sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);

    // Iterate through each rows one by one
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        // For each row, iterate through all the columns
        Iterator<Cell> cellIterator = row.cellIterator();

        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            // Check the cell type and format accordingly
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + "t");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "t");
                break;
            }
        }
        System.out.println("");
    }
    file.close();
} catch (Exception e) {
    e.printStackTrace();
}

错误输出:

Exception in thread “main” java.lang.NoSuchFieldError:
RAW_XML_FILE_HEADER at
org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179)
at
org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:228)
at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:93)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:294)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) at
org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:273)
at com.wtolliver.spring.test.ReadExcel.readExcel(ReadExcel.java:18)
at com.wtolliver.spring.test.App.main(App.java:17)

解决方法:

环顾四周后.我浏览了APACHE POI的文档,发现这是常量之一(不是我知道这意味着什么).
但最终,我意识到我使用的所有教程都是2014年之前的,所以我只是将我的Maven POM更改为apache POI 3.11和POI-OOXML.
它现在正在工作.

标签:nosuchfieldexception,java,xssf
来源: https://codeday.me/bug/20190722/1502789.html