Apache POI, using both XSSF and HSSF

前端 未结 4 1187
暖寄归人
暖寄归人 2021-01-02 13:07

I have a problem with Apache POI project.

I failed to use XSSF and HSSF in the \"Same Java Class\". Which jar should I dow

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 13:42

    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.");
    }
    

提交回复
热议问题