基于SXSSF (Streaming Usermodel API)的写文件
在POI3.8中SXSSF仅仅支持excel2007格式是对XSSF的一种流的扩展。目的在生成excel时候,需要生成大量的数据的时候,通过刷新的方式将excel内存信息刷新到硬盘的方式,提供写入数据的效率。
官方原文如下:
SXSSF (Streaming Usermodel API)
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.
测试代码如下:
- package com.easyway.excel.events.stream;
- import java.io.FileOutputStream;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.util.CellReference;
- import org.apache.poi.xssf.streaming.SXSSFWorkbook;
- /**
- * SXSSF (Streaming Usermodel API)
- * 当文件写入的流特别的大时候,会将内存中数据刷新flush到硬盘中,减少内存的使用量。
- * 起到以空间换时间作用,提供效率。
- *
- * @Title:
- * @version 1.0
- */
- public class SXSSExcelEvent {
- public static void main(String[] args) throws Throwable {
- //创建基于stream的工作薄对象的
- Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
- //SXSSFWorkbook wb = new SXSSFWorkbook();
- //wb.setCompressTempFiles(true); // temp files will be gzipped
- Sheet sh = wb.createSheet();
- //使用createRow将信息写在内存中。
- for(int rownum = 0; rownum < 1000; rownum++){
- Row row = sh.createRow(rownum);
- for(int cellnum = 0; cellnum < 10; cellnum++){
- Cell cell = row.createCell(cellnum);
- String address = new CellReference(cell).formatAsString();
- cell.setCellValue(address);
- }
- }
- // Rows with rownum < 900 are flushed and not accessible
- //当使用getRow方法访问的时候,将内存中的信息刷新到硬盘中去。
- for(int rownum = 0; rownum < 900; rownum++){
- System.out.println(sh.getRow(rownum));
- }
- // ther last 100 rows are still in memory
- for(int rownum = 900; rownum < 1000; rownum++){
- System.out.println(sh.getRow(rownum));
- }
- //写入文件中
- FileOutputStream out = new FileOutputStream("C://sxssf.xlsx");
- wb.write(out);
- //关闭文件流对象
- out.close();
- System.out.println("基于流写入执行完毕!");
- }
- }
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)的写文件的更多相关文章
- Structure Streaming和spark streaming原生API访问HDFS文件数据对比
此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Structure Stream访问方式 code examples import org.apache.sp ...
- Linux -- 基于zookeeper的java api(二)
Linux -- 基于zookeeper的java api(二) 写一个关于基于集群的zookeeper的自定义实现HA 基于客户端和监控器:使用监控的方法查看每个注册过的节点的状态来做出操作. Wa ...
- 详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能
文件同步传输工具比较多,传输的方式也比较多,比如:FTP.共享.HTTP等,我这里要讲的就是基于HTTP协议的WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器这样的一个工具(简称:一 ...
- 分享一个CQRS/ES架构中基于写文件的EventStore的设计思路
最近打算用C#实现一个基于文件的EventStore. 什么是EventStore 关于什么是EventStore,如果还不清楚的朋友可以去了解下CQRS/Event Sourcing这种架构,我博客 ...
- Windows API初练手 -- 疯狂写文件代码
警告:恶作剧软件,慎用!仅供初学者研究代码所用!!! 提示:默认文件创建目录在"D:\test",如果需要使用的话请自行更改目录. 1. Windows API 版本 (调用系统函 ...
- Apache Flink -Streaming(DataStream API)
综述: 在Flink中DataStream程序是在数据流上实现了转换的常规程序. 1.示范程序 import org.apache.flink.api.common.functions.FlatMap ...
- 苏宁基于Spark Streaming的实时日志分析系统实践 Spark Streaming 在数据平台日志解析功能的应用
https://mp.weixin.qq.com/s/KPTM02-ICt72_7ZdRZIHBA 苏宁基于Spark Streaming的实时日志分析系统实践 原创: AI+落地实践 AI前线 20 ...
- python 全栈开发,Day95(RESTful API介绍,基于Django实现RESTful API,DRF 序列化)
昨日内容回顾 1. rest framework serializer(序列化)的简单使用 QuerySet([ obj, obj, obj]) --> JSON格式数据 0. 安装和导入: p ...
- ecCodes 学习 利用ecCodes Python API对GRIB文件进行读写
参考 https://www.ecmwf.int/assets/elearning/eccodes/eccodes2/story_html5.htmlhttps://confluence.ecmwf. ...
随机推荐
- docker+tomcat+java配置(ubuntu)
原创文章,转载请注明出处. 本文是基于ubuntu14.04搭建的dosker,tomcat配置是在docker容器外面(为了方便查看日志)使用的是docker启动挂载,该tomcat工程依赖于doc ...
- Java security MD5加密算法
利用java.security对字符串进行MD5加密: import java.security.MessageDigest; import java.security.NoSuchAlgorithm ...
- 【原】.Net之美学习笔记-第1章-1.1.1值类型
结构还有一个特性:调用结构上的方法前,需要对其所有的字段进行赋值.
- AMD规范与CMD规范
JavaSript模块化 在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发? 模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...
- input效果:当鼠标在input中输入文字是改变内部文字效果
主要用到属性:onpropertychange事件(属性改变时触发的事件),oninput属性(当input有输入时发生的事件) onpropertychange事件是IE专属事件 oninput属性 ...
- javascript学习笔记3
一 判断下列数值中哪些于false相等? 0, 0.0, 0.000, -0, -0.0, 000, "0", "0.0", "0.000" ...
- (转载)SQL语句导入导出大全
SQL语句导入导出大全 /******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c ...
- Centos 6.2 32位机器安装新的JDK和Weblogic
一.首先卸载自带的JDK 1.查看自带的java版本. root@admin]#java -version java version "1.6.0" OpenJDK Runtime ...
- Java学习--final与static
final是java的关键字,它所表示的是“这部分是无法修改的”. 编译期常量,它在类加载的过程就已经完成了初始化,所以当类加载完成后是不可更改的,编译期可以将它代入到任何用到它的计算式中,也就是说可 ...
- Memcache的部署和使用
一.memcache简介 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. Mem ...