While Reading the data from Excel file with extens

2019-07-09 08:03发布

While reading the excel file with extension xlsx using apache poi it takes the long time for identifying the extension. Can you please help why it takes the long time?

if (file.getExcelFile().getOriginalFilename().endsWith("xls"))
    {
    workbook = new HSSFWorkbook(file.getExcelFile().getInputStream());
    } else if (file.getExcelFile().getOriginalFilename().endsWith("xlsx"))
    {
    workbook = new XSSFWorkbook(file.getExcelFile().getInputStream());
    } else {
    throw new IllegalArgumentException("Received file does not have a standard excel extension.");
    }

1条回答
可以哭但决不认输i
2楼-- · 2019-07-09 08:15

Promoting a comment to an answer - don't try to do this yourself, Apache POI has built-in code for doing this for you!

You should use WorkbookFactory.create(File) to do it, eg just

workbook = WorkbookFactory.create(file.getExcelFile());

As explained in the Apache POI docs, use a File directly in preference to an InputStream for quicker and lower memory processing

查看更多
登录 后发表回答