Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchFieldError when reading Excel sheet in java

I've followed a simple guide to constructing a workbook using Apache POI XSSF. Following the same guide I was able to WRITE an Excel sheet, however when attempting to read from one, I'm receiving the error displayed after the code.

Code:

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();
}

Error output:

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)

like image 569
William Tolliver Avatar asked Jun 03 '16 15:06

William Tolliver


People also ask

How to close XSSFWorkbook in Java?

Constructs a XSSFWorkbook object given a file name. Once you have finished working with the Workbook, you should close the package by calling close() , to avoid leaving file handles open.

What are the jars required to read Excel file in Java?

You need poi-3.12. jar to read XLS file and poi-ooxml-3.12. jar to read XLSX file in Java.

What is XSSFWorkbook in Java?

XSSFWorkbook. It is a class that is used to represent both high and low level Excel file formats. It belongs to the org. apache. xssf.


1 Answers

After looking around a bit. I browsed the documentation for APACHE POI, and saw that this was one of the constants (not that i know what that really means).

But eventually, I realized all the tutorials I used were pre-2014.

So I just changed my Maven POM to version 3.11 for both dependencies of apache-poi, and poi-ooxml.

Its working now.

like image 162
William Tolliver Avatar answered Oct 29 '22 20:10

William Tolliver