0

I am trying to read an Excel file using Apache POI but I get read error exception.

public class ReadExcelFileToList {

public static void main(String[] args) throws IOException {

    InputStream input = null;

    try {

        input = new FileInputStream(
                "C:\\Users\\jeet.chatterjee\\Downloads\\Book1.xls");

        System.out.println("file is found");
        POIFSFileSystem fs = new POIFSFileSystem(input);
        XSSFWorkbook wb = new XSSFWorkbook(input);
        XSSFSheet sheet = wb.getSheetAt(0);

        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            System.out.println("\n");
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {

                XSSFCell cell = (XSSFCell) cells.next();
                if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");

                else
                    System.out.print("Unknown cell type");

            }

        }

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    }

}

The exception log is

 file is found
     java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at java.io.PushbackInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.readFully(Unknown Source)
at java.util.zip.ZipInputStream.readLOC(Unknown Source)
at java.util.zip.ZipInputStream.getNextEntry(Unknown Source)
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:228)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:187)
at com.mj.test.ReadExcelFileToList.main(ReadExcelFileToList.java:32)

I get this error while trying to read this Excel file.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
lucifer
  • 2,297
  • 18
  • 58
  • 100

2 Answers2

1

You are using the same inputstream mutiple times in your code.i.e you are reading the same inputstream multiple times. That's why this error is thrown. You need to recreate the stream.

 input = new FileInputStream("C:\\Users\\jeet.chatterjee\\Downloads\\Book1.xls");
 //Using same inputstream is not correct
 //Comment the below line
 //POIFSFileSystem fs = new POIFSFileSystem(input);
 XSSFWorkbook wb = new XSSFWorkbook(input);

As a sidenote, you have to close the stream after its usage. I don't see it in your code.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • means i should not use the input object here?? – lucifer Jun 24 '14 at 04:40
  • Comment this line `POIFSFileSystem fs = new POIFSFileSystem(input);` I don't think you are using `fs` – Keerthivasan Jun 24 '14 at 04:43
  • means i should not use the input object here??but if so then how it will recognize the exel file?? – lucifer Jun 24 '14 at 04:47
  • @ Octopus Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:187) at com.mj.test.ReadExcelFileToList.main(ReadExcelFileToList.java:32) – lucifer Jun 24 '14 at 04:48
  • 1
    Check this one - http://stackoverflow.com/questions/6758364/getting-exceptionorg-apache-poi-openxml4j-exceptions-when-reading-xlsx-file-us – Keerthivasan Jun 24 '14 at 04:53
  • thank you for your help octopus but my problem is that i still need a generic way to read both xls and xlsx file,but in my code its only reading the xls file – lucifer Jun 24 '14 at 04:59
  • You are welcome, Glad it helped. Please enhance your code accordingly! – Keerthivasan Jun 24 '14 at 05:03
0

I am also try the same thing.. You should try this.. My excel is data.xlsx. You should use FileInputStream instead of POIFSFileSystem

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator; 
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JavaApplication3 {

    public static void main(String[] args) {
        try {
           FileInputStream file = new FileInputStream(new File("E:/data.xlsx"));
           XSSFWorkbook wb = new XSSFWorkbook(file);
           XSSFSheet sheet = wb.getSheetAt(0);
           Iterator rows = sheet.rowIterator();
           while (rows.hasNext()) {
             XSSFRow row = (XSSFRow) rows.next();
             System.out.println("\n");
             Iterator cells = row.cellIterator();
             while (cells.hasNext()) {

                XSSFCell cell = (XSSFCell) cells.next();
                if (XSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    System.out.print(cell.getNumericCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
                    System.out.print(cell.getStringCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
                    System.out.print(cell.getBooleanCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
                    System.out.print("BLANK     ");
                } else {
                    System.out.print("Unknown cell type");
                }
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
}
Subho
  • 921
  • 5
  • 25
  • 48