XSSFWorkbook takes a lot of time to load

喜夏-厌秋 提交于 2019-12-28 05:55:28

问题


I am using the following code:

File file = new File("abc.xlsx");
InputStream st = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(st);

The xlsx file itself has 25,000 rows and each row has content in 500 columns. During debugging, I saw that the third row where I create a XSSFWorkbook, it takes a lot of time (1 hour!) to complete this statement.

Is there a better way to access the values of the original xlsx file?


回答1:


First up, don't load a XSSFWorkbook from an InputStream when you have a file! Using an InputStream requires buffering of everything into memory, which eats up space and takes time. Since you don't need to do that buffering, don't!

If you're running with the latest nightly builds of POI, then it's very easy. Your code becomes:

File file = new File("C:\\D\\Data Book.xlsx");
OPCPackage opcPackage = OPCPackage.open(file);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);

Otherwise, it's very similar:

File file = new File("C:\\D\\Data Book.xlsx");
OPCPackage opcPackage = OPCPackage.open(file.getAbsolutePath());
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);



回答2:


Consider using the Streaming version of POI. This will load a subset of the file into memory as needed. It is the recommended method when dealing with large files.

POI SXSSF



来源:https://stackoverflow.com/questions/11154678/xssfworkbook-takes-a-lot-of-time-to-load

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