i am reading excel file using apache poi 3.16 here is my code
try
{
String excelPath = "C:\\Users\\wecme\\Desktop\\AccountStatement.xls";
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
// Create Workbook instance holding .xls file
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
// Get the first worksheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows
java.util.Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
// Get Each Row
Row row = rowIterator.next();
// Iterating through Each column of Each Row
java.util.Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
// Checking the cell format
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
}
}
System.out.println("");
}
} catch (IOException ie)
{
ie.printStackTrace();
}
my excel data looks like this:
Datetime Description TransactionId Credit Amount Debit Amount Remaining OdAmount EnteredBy Remarks
1/6/2017 8:14 IDEA (9542010237) COMMISSION GLGHQN 0.31 0 2721.92 0 iNHYD0390437LO IDEA COMMISSION
when i read other file which is not having time with date its reading properly ,but when i try to read this file this exception coming
Exception in thread "main" org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:286)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:758)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:291)
at excelRead.ReadExcel.main(ReadExcel.java:27)
please help me , thank you.
You're creating a
XSSFWorkbook
which is only suitable for working on xlsx files, i.e. the new xml based office format. Your file extension (xls) however suggests that this is the older Microsoft Office (2003 I guess) format. In order to fix the error you need to convert the file to xlsx format or you need to work with a HSSFWorkbookYou can find some samples concerning HSSF here: https://poi.apache.org/spreadsheet/examples.html#hssf-only Some examples concerning XDDF can be found here: https://poi.apache.org/spreadsheet/examples.html#xssf-only
According to the code where the Exception is thrown (see: https://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java?view=markup#l286)
And also the name of the package (openxml4j) it is required that the files are in an openxml compatible format. (i.e. xlsx)
In your case the library cannot open the file because it is not in a .zip format that it can open.
This question has also been asked two months ago, maybe this is also helpful:
How to fix NotOfficeXmlFileException in java Apache POI?