8

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)

Draken
  • 3,134
  • 13
  • 34
  • 54
William Tolliver
  • 305
  • 1
  • 2
  • 16
  • Are you sure that the Excel file is in good shape? Looks like it might be corrupted. – Matt Passell Jun 03 '16 at 15:43
  • 1
    This `NoSuchFieldError` stems mostly from a __version mismatch__ between [apache-poi dependencies](http://apache-poi.1045710.n5.nabble.com/java-lang-NoSuchFieldError-RAW-XML-FILE-HEADER-td5723977.html) – hc_dev Dec 29 '19 at 16:00

5 Answers5

15

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.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
William Tolliver
  • 305
  • 1
  • 2
  • 16
3

I got same error with different constant:

Exception in thread "main" java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK

Googled lot but no answer. I was using apache poi-ooxml version 3.11. Later I changed to version 3.17. Then it was working fine.

Hope this might helpful someone.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Kannan Msk
  • 157
  • 1
  • 6
1

In case if anyone working with the latest Apache POI library, make sure you add these dependencies and it works absolutely fine.

 <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.1.0</version>
        </dependency>
        <!-- log4j is optional-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.1</version>
        </dependency>
    </dependencies>

Java Snippet:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

public class Demo1 {

    private static final Logger logger = LogManager.getLogger(Demo1.class);

    public static void main(String[] args) {
        try {
            File file = new File("File location");
            FileInputStream fis = new FileInputStream(file);

            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet sheet = wb.getSheetAt(0);

            Iterator<Row> itr = sheet.iterator();
            logger.info("The given file is");
            while (itr.hasNext()) {
                Row row = itr.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                        case STRING:
                            logger.info(cell.getStringCellValue());
                            break;
                        case NUMERIC:
                            logger.info(cell.getNumericCellValue());
                            break;
                        default:
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Rajesh
  • 4,273
  • 1
  • 32
  • 33
0

I got the same error, try changing the XSSFWorkbook diclaration to HSSFWorkbook. It worked for me.

Murali
  • 9
  • 1
0

For the following error "java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK"

Please perform following task: upgrade the APACHE POI 3.** to the latest version in my case i have done the following thing i was previously using the apache poi 3.09 later i have upgraded the lib to latest version i.e 3.12 and its work !!

Perform the following steps and your code will but make sure the error should be same.