Purnima
Purnima

Reputation: 71

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!

Upvotes: 6

Views: 19278

Answers (3)

jlm1150
jlm1150

Reputation: 49

You don't necessarily need to pass a FileInputStream when creating a XSSFWorkbook object, you can also pass an absolute path+filename as a String and it can be quite long (works for me with 101 characters, 108 characters with the double slashes, could probably be longer). I just wrote a small local app under Windows 7 whose only argument is a property file containing (among other properties) the absolute path+filename of the .xlsx file that I want to deal with (example property format : datasetFile=C:\\Users\\jlm\\Documents\\Test Cases\\AAAS\\TestCase2JsonGenerator\\AAAS_g1.xlsx). I just pass the datasetFile property as parameter to the XSSFWorkbook constructor (example code line : wb = new XSSFWorkbook(tags.get("datasetFile")); ). It works just fine, BUT don't forget any of the double slashes otherwise you get the "Zip File is closed" exception (about 2 hours lost).

Upvotes: 0

Nick
Nick

Reputation: 1

Try shortening the file name and file path. Looks like there is a limit on how long the characters can be between " and ". It worked for me!

Upvotes: 0

Vicky
Vicky

Reputation: 3021

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...

Upvotes: 11

Related Questions