POI解决内存溢出问题
在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:
- * @Description: 实现TODO
- * @Copyright:Copyright (c) 2011
- * @Company:易程科技股份有限公司
- * @Date:2012-6-17
- * @author longgangbai
- * @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
POI解决内存溢出问题的更多相关文章
- android解决内存溢出的问题(没有从根本上解决)
Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...
- POI实现大数据EXCLE导入导出,解决内存溢出问题
使用POI能够导出大数据保证内存不溢出的一个重要原因是SXSSFWorkbook生成的EXCEL为2007版本,修改EXCEL2007文件后缀为ZIP打开可以看到,每一个Sheet都是一个xml文件, ...
- tomcat内存修改 解决内存溢出异常
有时候tomcat刚解压完,部署项目后运行会报内存溢出的错误. 原因:项目过大,tomcat内存小. 解决:找到[tomcat]/bin/catlina.bat文件,打开: 在@echo off上面( ...
- Tomcat参数设置,解决内存溢出问题
Tomcat默认参数不适合生产环境使用,因此需要修改一些参数 1.修改启动时内存参数.并指定JVM时区 (在Windows Server 2008 下时间少了8个小时): 在Tomcat上运行j2ee ...
- Linux下jenkins改端口、解决内存溢出、版本升级
1.新版本的jenkins修改端口新版本jenkins的配置文件在/etc/sysconfig/jenkinsvi /etc/sysconfig/jenkins找到JENKINS_PORT=" ...
- Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题
最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...
- 在Android中解决内存溢出 – OutOfMemoryError
原文链接:http://riggaroo.co.za/fixing-memory-leaks-in-android-outofmemoryerror/ 注:本文在原文基础上在如何判断内存是否泄露方面进 ...
- c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter
1.XmlTextWriter 表示提供快速.非缓存.只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文 ...
- solrCloud设置Tomcat jvm内存解决内存溢出的问题
几乎已经搜遍了整个网络,没有找到一篇解决设置solr在Tomcat下设置虚拟机内存的文章. 因为之前一直是在Tomcat中设置zkhost参数,在加上jvm参数后会无法启动,添加其他参数也没有生效 ...
随机推荐
- 在线模拟http-post请求
今天,要测试一个post请求的API,找了下,下面的网站可以直接利用起来,mark下 http://www.atool.org/httptest.php
- PHP 缓存详解
为什么要使用缓存 一个网站或者一个应用的标准流程是浏览器向应用服务器发出请求,应用服务器做一些计算和逻辑判断之后再请求数据库,数据库收到请求后在经过计算将数据返回给应用服务器,应用服务器再次计算后把数 ...
- finger 工具:用来查询用户信息,侧重用户家目录、登录SHELL等
finger 工具侧重于用户信息的查询:查询的内容包括用户名(也被称为登录名Login),家目录,用户真实的名字(Name)... ... 办公地址.办公电话:也包括登录终端.写状态.空闭时间等: 我 ...
- np.random.choice的用法
np.random.choice的用法 2018年01月15日 10:18:23 qfpkzheng 阅读数:6306 标签: 自己学习 更多 个人分类: 总结 import numpy as n ...
- Docker(一)简介及核心概念
1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像: 运行中的这 ...
- Effective Modern C++ 条款2:理解auto型别推导
在条款1中,我们已经了解了有关模板型别的推导的一切必要知识,那么也就意味着基本上了解了auto型别推导的一切必要知识. 因为,除了一个奇妙的例外情况,auto型别推导就是模板型别推导.尽管和模板型别推 ...
- linux和window双系统下修改系统启动项
参考:http://jingyan.baidu.com/article/63acb44ae4062c61fcc17e27.html: 我们在安装双系统之后经常会遇到想打开windows但默认启动项是u ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- redis 原生操作 & python操作redis
一.基本介绍 1.简介 Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...
- Leetcode686.Repeated String Match重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...