今天看poi的官方文档时,发现官方做了一个Files和InputStreams的对比,创建WorkBook时,推荐读取file。
Files vs InputStreams
When opening a workbook, either a .xls HSSFWorkbook, or a .xlsx XSSFWorkbook, the Workbook can be loaded from either a File or an InputStream. Using a Fileobject allows for lower memory consumption, while an InputStream requires more memory as it has to buffer the whole file.(当打开一个workbook时, .xls HSSFWorkbook或 .xlsx XSSFWorkbook,这个Workbook 能从一个File或InputStream中载入。使用一个文件对象可以降低内存消耗,而一个输入流需要更多的内存,它必须缓存整个文件。)
If using WorkbookFactory, it's very easy to use one or the other:
// Use a file Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // Use an InputStream, needs more memory Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));