Need help reading apache poi xls using XSSF

I need help reading xls files using apache XSSF.

The XSSF implementation works fine for xlsx. Does not work for xls files.

Here is the code:

    XSSFWorkbook workBook = new XSSFWorkbook("fileName");
    XSSFSheet sheet = workBook.getSheetAt(0);
    XSSFRow row = sheet.getRow(0);

Any workaround is welcome.

+5
source share
2 answers

Instead of using the XSSF classes directly, you should use interfaces that are common between HSSF (.xls) and XSSF (.xlsx). Then the code snippet from your question will become as follows:

 Workbook wb = WorkbookFactory.create(file); // Or InputStream
 Sheet sheet = workBook.getSheetAt(0);
 Row row = sheet.getRow(0);
 Cell cell = row.getCell(0);
 System.out.println("Cell A1 is of type " + cell.getCellType());

See Apache POI QuickGuide for more information and examples .

+10
source

Try it...

FileInputStream is = new FileInputStream(filePath))
XSSFWorkbook workbook = new XSSFWorkbook(is);
is.close();

This link below seems to have a solution to this problem ... Good luck.

http://apache-poi.1045710.n5.nabble.com/InvalidOperationException-Can-t-open-specified-file-td5524067.html

-1

All Articles