While Reading the data from Excel file with extension xlsx using apache poi it takes long time

跟風遠走 提交于 2019-12-11 02:58:02

问题


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:


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



来源:https://stackoverflow.com/questions/39567127/while-reading-the-data-from-excel-file-with-extension-xlsx-using-apache-poi-it-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!