码迷,mamicode.com
首页 > 其他好文 > 详细

POI解决内存溢出问题

时间:2015-07-30 14:48:28      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:

 在POI3.8中SXSSF仅仅支持excel2007格式是对XSSF的一种流的扩展。目的在生成excel时候,需要生成大量的数据的时候,通过刷新的方式将excel内存信息刷新到硬盘的方式,提供写入数据的效率。

官方原文如下:

SXSSF (Streaming Usermodel API)

Note
          SXSSF is a brand new contribution and some features were added after it was first introduced in POI 3.8-beta3. Users are advised to try the latest build from trunk. Instructions how to build are here .

SXSSF (package: org.apache.poi.xssf.streaming) is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limite d. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.

                 You can specify the window size at workbook construction time via new SXSSFWorkbook(int windowSize) or you can set it per-sheet via SXSSFSheet#setRandomAccessWindowSize(int windowSize)

When a new row is created via createRow() and the total number of unflushed records would exceed the specified window size, then the row with the lowest index value is flushed a nd cannot be accessed via getRow() anymore.

                   The default window size is 100 and defined by SXSSFWorkbook.DEFAULT_WINDOW_SIZE.

A windowSize of -1 indicates unlimited access. In this case all records that have not been flushed by a call to flushRows() are available for random access.

The example below writes a sheet with a window of 100 rows. When the row count reaches 101, the row with rownum=0 is flushed to disk and removed from memory, when rownum reaches 102 then the row with rownum=1 is flushed, etc.

 

测试代码如下:

Java代码  技术分享
  1. package com.easyway.excel.events.stream;  
  2.   
  3. import java.io.FileOutputStream;  
  4.   
  5. import org.apache.poi.ss.usermodel.Cell;  
  6. import org.apache.poi.ss.usermodel.Row;  
  7. import org.apache.poi.ss.usermodel.Sheet;  
  8. import org.apache.poi.ss.usermodel.Workbook;  
  9. import org.apache.poi.ss.util.CellReference;  
  10. import org.apache.poi.xssf.streaming.SXSSFWorkbook;  
  11. /** 
  12.  * SXSSF (Streaming Usermodel API) 
  13.  *     当文件写入的流特别的大时候,会将内存中数据刷新flush到硬盘中,减少内存的使用量。 
  14.  * 起到以空间换时间作用,提供效率。 
  15.  *  
  16.  * @Title:  
  17.  * @Description: 实现TODO 
  18.  * @Copyright:Copyright (c) 2011 
  19.  * @Company:易程科技股份有限公司 
  20.  * @Date:2012-6-17 
  21.  * @author  longgangbai 
  22.  * @version 1.0 
  23.  */  
  24. public class SXSSExcelEvent {  
  25.     public static void main(String[] args) throws Throwable {  
  26.         //创建基于stream的工作薄对象的  
  27.         Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk  
  28.         //SXSSFWorkbook wb = new SXSSFWorkbook();   
  29.         //wb.setCompressTempFiles(true); // temp files will be gzipped  
  30.         Sheet sh = wb.createSheet();  
  31.         //使用createRow将信息写在内存中。  
  32.         for(int rownum = 0; rownum < 1000; rownum++){  
  33.             Row row = sh.createRow(rownum);  
  34.             for(int cellnum = 0; cellnum < 10; cellnum++){  
  35.                 Cell cell = row.createCell(cellnum);  
  36.                 String address = new CellReference(cell).formatAsString();  
  37.                 cell.setCellValue(address);  
  38.             }  
  39.   
  40.         }  
  41.   
  42.         // Rows with rownum < 900 are flushed and not accessible  
  43.         //当使用getRow方法访问的时候,将内存中的信息刷新到硬盘中去。  
  44.         for(int rownum = 0; rownum < 900; rownum++){  
  45.           System.out.println(sh.getRow(rownum));  
  46.         }  
  47.   
  48.         // ther last 100 rows are still in memory  
  49.         for(int rownum = 900; rownum < 1000; rownum++){  
  50.             System.out.println(sh.getRow(rownum));  
  51.         }  
  52.         //写入文件中  
  53.         FileOutputStream out = new FileOutputStream("C://sxssf.xlsx");  
  54.         wb.write(out);  
  55.         //关闭文件流对象  
  56.         out.close();  
  57.         System.out.println("基于流写入执行完毕!");  
  58.     }  
  59. }  

 

         SXSSF flushes sheet data in temporary files (a temp file per sheet) and the size of these temporary files can grow to a very large value . For example, for a 20 MB csv data the size of the temp xml becomes more than a gigabyte. If the size of the temp files is an issue, you can tell SXSSF to use gzip compression:

  SXSSFWorkbook wb = new SXSSFWorkbook(); 
  wb.setCompressTempFiles(true); // temp files will be gzipped

POI解决内存溢出问题

标签:

原文地址:http://www.cnblogs.com/caogang/p/4689292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!