简体   繁体   English

需要帮助apache poi用XSSF阅读xls

[英]Need help for apache poi reading xls with XSSF

I need help reading xls files using apache XSSF. 我需要帮助使用apache XSSF读取xls文件。

The implementation of XSSF is working fine for "xlsx". XSSF的实现适用于“xlsx”。 Not working for "xls" files. 不适用于“xls”文件。

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). 您应该使用HSSF(.xls)和XSSF(.xlsx)之间通用的接口,而不是直接使用XSSF类。 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 有关更多信息和示例,请参阅Apache POI QuickGuide

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 http://apache-poi.1045710.n5.nabble.com/InvalidOperationException-Can-t-open-specified-file-td5524067.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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