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
相關文章
相關標籤/搜索