在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. iOS中的定时器 - NSTimer 使用方法

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3556805.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  2. 九度OJ 1532 棋盘寻宝扩展 -- 动态规划【背包问题】

    题目地址:http://ac.jobdu.com/problem.php?pid=1532 题目描述: 现在有一个8*8的棋盘,上面放着64个不同价值的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大 ...

  3. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...

  4. Eclipse配置CAS server

    1.下载cas server的源码包(我使用的是cas-server-3.5.2.1-release.zip) 2.解压压缩包到某个目录下,找到cas-server-3.5.2.1-release.z ...

  5. 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考

    http://www.cnblogs.com/insus/p/4142264.html 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考 首先普世的编程架 ...

  6. asp.net弹出框后页面走样

    1.去掉language='javascript' ,问题依旧 2.后面加上Response.Write("<script>document.location=document. ...

  7. [C#]对象深拷贝

    关键代码: /// <summary> /// 对象深拷贝 /// </summary> /// <typeparam name="T">泛型& ...

  8. Django同步创建models table失败

    django1.8通过manage.py syncdb 执行同步创建models中创建的表格失败 由于syncdb命令在1.9版本中会被remove, 需要改用makemigrations命令进行代替 ...

  9. 手动将MySQL服务安装到windows中

    我的系统是win7 x64,mysql版本5.6 (网上的老教程很多都没用了,版本太旧,这是我自己总结出来的方法) 下载zip包的mysql可以获得最新版本,还可以免安装,好处多多,但是要把mysql ...

  10. centos不能挂在ntfs

    root@s 下载]# mount /dev/sdb1 /mnt mount: unknown filesystem type 'ntfs' wget http://www.tuxera.com/co ...