简体   繁体   中英

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.

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

Try this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM