基于SXSSF (Streaming Usermodel API)的写文件

Posted Fight With Me!!!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于SXSSF (Streaming Usermodel API)的写文件相关的知识,希望对你有一定的参考价值。

在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.  * @version 1.0 
  18.  */  
  19. public class SXSSExcelEvent {  
  20.     public static void main(String[] args) throws Throwable {  
  21.         //创建基于stream的工作薄对象的  
  22.         Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk  
  23.         //SXSSFWorkbook wb = new SXSSFWorkbook();   
  24.         //wb.setCompressTempFiles(true); // temp files will be gzipped  
  25.         Sheet sh = wb.createSheet();  
  26.         //使用createRow将信息写在内存中。  
  27.         for(int rownum = 0; rownum < 1000; rownum++){  
  28.             Row row = sh.createRow(rownum);  
  29.             for(int cellnum = 0; cellnum < 10; cellnum++){  
  30.                 Cell cell = row.createCell(cellnum);  
  31.                 String address = new CellReference(cell).formatAsString();  
  32.                 cell.setCellValue(address);  
  33.             }  
  34.   
  35.         }  
  36.   
  37.         // Rows with rownum < 900 are flushed and not accessible  
  38.         //当使用getRow方法访问的时候,将内存中的信息刷新到硬盘中去。  
  39.         for(int rownum = 0; rownum < 900; rownum++){  
  40.           System.out.println(sh.getRow(rownum));  
  41.         }  
  42.   
  43.         // ther last 100 rows are still in memory  
  44.         for(int rownum = 900; rownum < 1000; rownum++){  
  45.             System.out.println(sh.getRow(rownum));  
  46.         }  
  47.         //写入文件中  
  48.         FileOutputStream out = new FileOutputStream("C://sxssf.xlsx");  
  49.         wb.write(out);  
  50.         //关闭文件流对象  
  51.         out.close();  
  52.         System.out.println("基于流写入执行完毕!");  
  53.     }  
  54. }  

 

         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


以上是关于基于SXSSF (Streaming Usermodel API)的写文件的主要内容,如果未能解决你的问题,请参考以下文章

POI 生成excel(大数据量) SXSSF

如何使用 Apache POI (SXSSF) 为特定单元格设置数据(数字)格式区域设置?

SXSSF 工作簿 createSheet() 在 linux 环境中失败

HSSFXSSF和SXSSF区别以及Excel导出优化

HSSF XSSF SXSSF

使用 SXSSF POI 编写大型 Excel 工作表