Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help for apache poi reading xls with XSSF

I need help reading xls files using apache XSSF.

The implementation of XSSF is working fine for "xlsx". Not working 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 appreciated.

like image 825
Kiran T Avatar asked Apr 18 '12 15:04

Kiran T


People also ask

Can XSSF read XLS file?

XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.

Does Apache POI support XLS?

Apache POI is your Java Excel solution (for Excel 97-2008). We have a complete API for porting other OOXML and OLE2 formats and welcome others to participate. OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats.


1 Answers

Rather than using XSSF classes directly, you should use the interfaces that are common between both HSSF (.xls) and XSSF (.xlsx). The snippet of code from your question would then become:

 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 the Apache POI QuickGuide for more information and examples

like image 123
Gagravarr Avatar answered Sep 28 '22 04:09

Gagravarr