基于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. ...
随机推荐
- socket通信_笔记
(socket通信) 客户端与服务器端通信问题: 我们首先要了解一个概念性的词汇:Socket socket的英文原义是“孔”或“插座”.作为进程通信机制,取后一种意思.通常也称作“套接字”,用于描述 ...
- hdu 5056 Boring count
贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...
- PH获取当前url路径及服务器路径汇总 (url 获取当前路径 服务器路径)
以下是整理的一些, php中获取路径的小知识, 希望对你有所帮助! 1,$_SERVER["QUERY_STRING"] 说明:查询(query)的字符串 2,$_SERVER[& ...
- 如何使用 require.js ,实现js文件的异步加载,避免网页失去响应,管理模块之间的依赖性,便于代码的编写和维护。
一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...
- ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践
写在前面 :从提出需求到完美的解决问题,实现过程是曲折的. 需求:在前(web client)后(Restful Service)端完全解耦的模式框架下,webclient需要请求 Service 返 ...
- iOS局部刷新
iOS: TableView如何刷新指定的cell 或section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithInd ...
- shopnc 商城源码阅读笔记-缓存技术
缓存方式 : 从 shopnc 的缓存驱动目录 /framework/cache里已有的实现类来看,shopnc支持以下5种缓存方式 apc Eaccelerator file memcache xc ...
- C#多线程(二)
一.线程池 每次创建一个线程,都会花费几百微秒级别的时间来创建一个私有的局部栈,每个线程默认使用1M的内存.这个可以在使用Thread类的构造函数时设置: new Thread(new ThreadS ...
- mysql索引使用笔记
1.使用explain语句查看性能mysql> explain select product_id from orders where order_id in (123, 312, 223, 1 ...
- DevExpress VCL 一键安装工具
一键安装工具 DxAutoInstaller-2.1.3 For Devexpress VCL:http://download.csdn.net/detail/wozengcong/8396181 一 ...