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参数后会无法启动,添加其他参数也没有生效 ...
随机推荐
- js中的自执行匿名函数 (function(){})()
JS函数有两种命名方式 1.声明式 声明式会导致函数提升,function会被解释器优先编译.即我们用声明式写函数,可以在任何区域声明,不会影响我们调用. function XXX(){} 2.函数表 ...
- LINUX用户身份切换
Su 命令作用 su的作用是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码. 使用方式 su [-fmp] [-c command] [-s shell] [--help] [--ver ...
- javascript基础:事件
事件: 概念:某些组件被执行了某些操作后,触发某些代码的执行 * 事件:某些操作,如:单击,双击,键盘按下了,鼠标移动了 * 事件源:组件.如:按钮 文本输入框.... * 监听器:代码 * ...
- Armijo线性搜索
用“人话”解释不精确线搜索中的Armijo-Goldstein准则及Wolfe-Powell准则 line search(一维搜索,或线搜索)是最优化(Optimization)算法中的一个基础步骤/ ...
- python 与 selenium 学习笔记
在写自动运行测试用例,并且生成HTML报告的时候,遇到了这个报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in positi ...
- python 中的split()函数和os.path.split()函数
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- HTTP_REFERER的用法及伪造
引言 在php中,可以使用$_SERVER[‘HTTP_REFERER’]来获取HTTP_REFERER信息,关于HTTP_REFERER,php文档中的描述如下: “引导用户代理到当前页的前一页的地 ...
- UML类图解释
那个动物矩形框,它就代表一个类(Class).类图分三层,第一层显示类的名称,如果是抽象类,则就用斜体显示.第二层是类的特性,通常是字段和属性.第三层是类的操作,通常是方法或行为.注意前面的符号,“+ ...
- Unity 同一Text文本修改不同的字体大小和字体颜色
类似Html,在color和size对应的<>str</>中,就能修改str的相关属性, 下面的代码就是把time改为字体颜色为红色,大小为40: 而前面的"Time ...
- XtraBackup构建MySQL主从环境的方法
环境:HE3主库,HE1从库HE1:192.168.1.248HE3:192.168.1.250从库my.cnf加入以下参数并重启数据库:read_only=1log_slave_updates=1( ...