Apache POI, using both XSSF and HSSF

廉价感情. 提交于 2019-11-30 20:12:08

Instead of doing that, try using the new release of Apache POI 3.7, it has SS package which handles both HSSF and XSSF without worrying about type

Details here: http://poi.apache.org/spreadsheet/index.html

Aside from the "standard" SS package solution, you can also simply use an if statement to correctly load the right workbook format into an Workbook interface object, like so:

Workbook workbook; //<-Interface, accepts both HSSF and XSSF.
File file = new File("YourExcelFile.xlsx");
if (FileUtils.getFileExt(file).equalsIgnoreCase("xls")) {
  workbook = new HSSFWorkbook(new FileInputStream(file));
} else if (FileUtils.getFileExt(file).equalsIgnoreCase("xlsx")) {
  workbook = new XSSFWorkbook(new FileInputStream(file));
} else {
  throw new IllegalArgumentException("Received file does not have a standard excel extension.");
}
vingin

Use the factory instead which handles both xssf and hssf

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Workbook wb = WorkbookFactory.create(new File("file"))

This worked for me:

    filePath = "C:\Users\user1\workspace\myproject\Excel.xlsx"
    String extension = FilenameUtils.getExtension(filePath);
    System.out.println(extension);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!