本文介绍了使用 apache poi 从扩展名为 xlsx 的 Excel 文件中读取数据时需要很长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 apache poi 读取扩展名为 xlsx 的 excel 文件时,识别扩展名需要很长时间.你能帮忙解释一下为什么需要很长时间吗?

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

推荐答案

将评论推广到答案 - 不要尝试自己执行此操作,Apache POI 已内置代码可以为您执行此操作!

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

你应该使用 WorkbookFactory.create(File) 来做,例如只是

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

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

Apache POI 文档中所述,直接使用文件而不是使用文件InputStream 用于更快、更低的内存处理

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

这篇关于使用 apache poi 从扩展名为 xlsx 的 Excel 文件中读取数据时需要很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 10:18