Apache POI, using both XSSF and HSSF

前端 未结 4 1154
暖寄归人
暖寄归人 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:21

    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

    0 讨论(0)
  • 2021-01-02 13:29

    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"))
    
    0 讨论(0)
  • 2021-01-02 13:34

    This worked for me:

        filePath = "C:\Users\user1\workspace\myproject\Excel.xlsx"
        String extension = FilenameUtils.getExtension(filePath);
        System.out.println(extension);
    
    0 讨论(0)
  • 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.");
    }
    
    0 讨论(0)
提交回复
热议问题