POI解决内存溢出问题

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

官方原文以下:html

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.java

                 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)apache

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.dom

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

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.ui

 

测试代码以下:this

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:spa

  SXSSFWorkbook wb = new SXSSFWorkbook(); 
  wb.setCompressTempFiles(true); // temp files will be gzipped
相关文章
相关标签/搜索