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

测试代码如下:

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

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 Beta 3.8下,会有临时文件产生,比如: 
poi-sxssf-sheet4654655121378979321.xml
文件位置:java.io.tmpdir这个环境变量下的位置
Windows 7下是C:\Users\xxxxxAppData\Local\Temp
Linux下是 /var/tmp/

另外:SXSSFWORKBOOK只能用于导出,无法用于导入。

基于SXSSF (Streaming Usermodel API)的写文件的更多相关文章

  1. Structure Streaming和spark streaming原生API访问HDFS文件数据对比

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Structure Stream访问方式 code examples import org.apache.sp ...

  2. Linux -- 基于zookeeper的java api(二)

    Linux -- 基于zookeeper的java api(二) 写一个关于基于集群的zookeeper的自定义实现HA 基于客户端和监控器:使用监控的方法查看每个注册过的节点的状态来做出操作. Wa ...

  3. 详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能

    文件同步传输工具比较多,传输的方式也比较多,比如:FTP.共享.HTTP等,我这里要讲的就是基于HTTP协议的WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器这样的一个工具(简称:一 ...

  4. 分享一个CQRS/ES架构中基于写文件的EventStore的设计思路

    最近打算用C#实现一个基于文件的EventStore. 什么是EventStore 关于什么是EventStore,如果还不清楚的朋友可以去了解下CQRS/Event Sourcing这种架构,我博客 ...

  5. Windows API初练手 -- 疯狂写文件代码

    警告:恶作剧软件,慎用!仅供初学者研究代码所用!!! 提示:默认文件创建目录在"D:\test",如果需要使用的话请自行更改目录. 1. Windows API 版本 (调用系统函 ...

  6. Apache Flink -Streaming(DataStream API)

    综述: 在Flink中DataStream程序是在数据流上实现了转换的常规程序. 1.示范程序 import org.apache.flink.api.common.functions.FlatMap ...

  7. 苏宁基于Spark Streaming的实时日志分析系统实践 Spark Streaming 在数据平台日志解析功能的应用

    https://mp.weixin.qq.com/s/KPTM02-ICt72_7ZdRZIHBA 苏宁基于Spark Streaming的实时日志分析系统实践 原创: AI+落地实践 AI前线 20 ...

  8. python 全栈开发,Day95(RESTful API介绍,基于Django实现RESTful API,DRF 序列化)

    昨日内容回顾 1. rest framework serializer(序列化)的简单使用 QuerySet([ obj, obj, obj]) --> JSON格式数据 0. 安装和导入: p ...

  9. ecCodes 学习 利用ecCodes Python API对GRIB文件进行读写

    参考 https://www.ecmwf.int/assets/elearning/eccodes/eccodes2/story_html5.htmlhttps://confluence.ecmwf. ...

随机推荐

  1. 第32条:用EnumSet代替位域

    如果一个枚举类型的元素主要用在集合中,一般使用int枚举模式,将2的不同倍数赋予每个常量: public class Text { public static final int STYLE_BOLD ...

  2. jquery 100%全屏自适应宽可点击左右和焦点的自动切换幻灯片特效

    http://www.divcss5.com/css-texiao/texiao717.shtml http://d.divcss5.com/divcss5/down/2014062201.zip

  3. 组织http请求

    post方式 string stratTime=""; string end=""://要拼接的参数 string postURL = "http:/ ...

  4. FontDialog组件设置字体

    1.设置字体 private void button3_Click(object sender, EventArgs e) { this.fontDialog1.ShowDialog(); this. ...

  5. org.springframework.web.servlet.view.InternalResourceViewResolver

    http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...

  6. 全面认识UML类图元素

    本节和大家一起学习一下UML类图元素,类图能出色地表示继承与合成关系.为了将UML类图作为一种高效的沟通工具使用,开发者必须理解如何将类图上出现的元素转换到Java中.请看本节详细介绍. 全面认识UM ...

  7. 魔兽争霸Ⅲ运行时不能初始化directX的错误解决

    运行魔兽争霸3不能初始化DirectX错误这样解决: 1:在运行中输入(win+r):dxdiag,查看显示栏,确定电脑已安装好directx 8.1以上,且下面的三个加速都已开启. 2:如果没有安装 ...

  8. poj 2440 (找递推公式)

    http://poj.org/problem?id=2440 DNA Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3254 ...

  9. NetWorker常用术语

    术语 描述 active group 每24小时至少计划备份一次的备份组. Advanced File Type Device (AFTD) 为大容量磁盘设备和并发操作设计的磁盘备份设备配置选项. A ...

  10. python里面的几个编码函数

    时不时的被编码折腾一下 >>> print urllib.quote('中国') %E4%B8%AD%E5%9B%BD >>> s = '%E4%B8%AD%E5% ...