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!
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With