IAmYourFaja
IAmYourFaja

Reputation: 56944

Apache POI throwing IOException when reading XLSX workbook

I'm trying to get the following code to run and am getting an IOException:

String cellText = null;
InputStream is = null;
try {
    // Find /mydata/myworkbook.xlsx
    is = new FileInputStream("/mydata/myworkbook.xlsx");
    is.close();

    System.out.println("Found the file!");

    // Read it in as a workbook and then obtain the "widgets" sheet.
    Workbook wb = new XSSFWorkbook(is);
    Sheet sheet = wb.getSheet("widgets");

    System.out.println("Obtained the widgets sheet!");

    // Grab the 2nd row in the sheet (that contains the data we want).
    Row row = sheet.getRow(1);

    // Grab the 7th cell/col in the row (containing the Plot 500 English Description).
    Cell cell = row.getCell(6);
    cellText = cell.getStringCellValue();

    System.out.println("Cell text is: " + cellText);
} catch(Throwable throwable) {
    System.err.println(throwable.getMessage());
} finally {
    if(is != null) {
        try {
            is.close();
        } catch(IOException ioexc) {
            ioexc.printStackTrace();
        }
    }
}

The output from running this in Eclipse is:

Found the file!
Stream Closed
java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:236)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
    at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:414)
    at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:91)
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
    at me.myorg.MyAppRunner.run(MyAppRunner.java:39)
    at me.myorg.MyAppRunner.main(MyAppRunner.java:25)

The exception is coming from the line:

Workbook wb = new XSSFWorkbook(is);

According to the XSSFWorkbook Java Docs this is a valid constructor for an XSSFWorkbook object, and I don't see anything "jumping out" at me to indicate that I'm using my InputStream incorrectly. Can any POI gurus help spot where I'm going awrye? Thanks in advance.

Upvotes: 0

Views: 6631

Answers (3)

Gagravarr
Gagravarr

Reputation: 48376

As the others have pointed out, you are closing your InputStream which is breaking things

However, you really shouldn't be using an InputStream in the first place! POI uses less memory when given the File object directly rather than going through an InputStream.

I'd suggest you have a read through the POI FAQ on File vs InputStream, then change your code to be:

OPCPackage pkg = OPCPackage.open(new File("/mydata/myworkbook.xlsx"));
Workbook wb = new XSSFWorkbook(pkg);

Upvotes: 1

Gary
Gary

Reputation: 916

The problem is simple:

is = new FileInputStream("/mydata/myworkbook.xlsx");
is.close();

You are closing your output stream before passing it to the constructor and it cannot be read.

Simply delete the is.close() here to fix the issue, as it will be cleaned up in the finally statement at the end.

Upvotes: 6

NimChimpsky
NimChimpsky

Reputation: 47300

you are closing the stream is.close();

and then using it, don't close it until you have used it.

Upvotes: 2

Related Questions