Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When reading .xlsx file using POI I got an error "Zip File is closed"

public Sheet readExcel() throws Exception{
    //File fi=new File(new File(System.getProperty("user.dir"))+"\\src\\testdata2.xls");
    File fi=new File("C:\\Users\\admin\\workspace\\HMS\\src\\testdata\\testdata1.xlsx");

    Workbook wb = new XSSFWorkbook(fi);
    Sheet  Sheet = wb.getSheetAt(0);

    int rowCount = Sheet.getLastRowNum()-Sheet.getFirstRowNum();

    for (int i = 1; i < rowCount+1; i++) {
        Row row = Sheet.getRow(i);

        if(row.getCell(0).toString().length()==0){

            System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+

            row.getCell(3).toString()+"----"+ row.getCell(4).toString());
        }
    }    
    return Sheet;   
}

By running above code am getting error like this........

Exception in thread "main" java.lang.IllegalStateException: Zip File is closed at org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45) at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:186) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:254) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:201) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:294) at ExcelReader.readExcel(ExcelReader.java:16) at ExcelReader.main(ExcelReader.java:30)

Can anyone help me tracing out what exactly is the problem.

I Googled but couldn't get the solution!

like image 833
Purnima Avatar asked May 24 '15 12:05

Purnima


People also ask

How do I open an XLSX file with zip?

Xlsx files are just ZIP files, so you can simply unzip them right away using your favourite ZIP tool. Open your zip file tool, like for example, Winrar, find your excel file there, right-click on it, then you can simply "extract to a specified folder" and then your xml files will be saved there.

Does Apache POI support Xlsx?

The Apache POI library supports both . xls and . xlsx files and is a more complex library than other Java libraries for working with Excel files.

Does Apache POI help to read Excel file?

To Read and Write Excel file in Java, Apache provides a very famous library POI. This library is capable enough to read and write both XLS and XLSX file format of Excel. To read XLS files, an HSSF implementation is provided by POI library.


1 Answers

To read an xslx file use create an object of FileInputStream class

    //Create a object of File class to open xlsx file

    File file = new File("path/filename.xlsx");

    //Create an object of FileInputStream class to read excel file

    FileInputStream inputStream = new FileInputStream(file);

    //create object of XSSFWorkbook class

    Workbook wb = new XSSFWorkbook(inputStream);

Hope this heps you...

like image 53
Vicky Avatar answered Nov 14 '22 21:11

Vicky