在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. * @Description: 实现TODO
  16. * @Copyright:Copyright (c) 2011
  17. * @Company:易程科技股份有限公司
  18. * @Date:2012-6-17
  19. * @author  longgangbai
  20. * @version 1.0
  21. */
  22. public class SXSSExcelEvent {
  23. public static void main(String[] args) throws Throwable {
  24. //创建基于stream的工作薄对象的
  25. Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
  26. //SXSSFWorkbook wb = new SXSSFWorkbook();
  27. //wb.setCompressTempFiles(true); // temp files will be gzipped
  28. Sheet sh = wb.createSheet();
  29. //使用createRow将信息写在内存中。
  30. for(int rownum = 0; rownum < 1000; rownum++){
  31. Row row = sh.createRow(rownum);
  32. for(int cellnum = 0; cellnum < 10; cellnum++){
  33. Cell cell = row.createCell(cellnum);
  34. String address = new CellReference(cell).formatAsString();
  35. cell.setCellValue(address);
  36. }
  37. }
  38. // Rows with rownum < 900 are flushed and not accessible
  39. //当使用getRow方法访问的时候,将内存中的信息刷新到硬盘中去。
  40. for(int rownum = 0; rownum < 900; rownum++){
  41. System.out.println(sh.getRow(rownum));
  42. }
  43. // ther last 100 rows are still in memory
  44. for(int rownum = 900; rownum < 1000; rownum++){
  45. System.out.println(sh.getRow(rownum));
  46. }
  47. //写入文件中
  48. FileOutputStream out = new FileOutputStream("C://sxssf.xlsx");
  49. wb.write(out);
  50. //关闭文件流对象
  51. out.close();
  52. System.out.println("基于流写入执行完毕!");
  53. }
  54. }

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:

  1. SXSSFWorkbook wb = new SXSSFWorkbook();
  2. wb.setCompressTempFiles(true); // temp files will be gzipped

POI解决内存溢出问题的更多相关文章

  1. android解决内存溢出的问题(没有从根本上解决)

    Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...

  2. POI实现大数据EXCLE导入导出,解决内存溢出问题

    使用POI能够导出大数据保证内存不溢出的一个重要原因是SXSSFWorkbook生成的EXCEL为2007版本,修改EXCEL2007文件后缀为ZIP打开可以看到,每一个Sheet都是一个xml文件, ...

  3. tomcat内存修改 解决内存溢出异常

    有时候tomcat刚解压完,部署项目后运行会报内存溢出的错误. 原因:项目过大,tomcat内存小. 解决:找到[tomcat]/bin/catlina.bat文件,打开: 在@echo off上面( ...

  4. Tomcat参数设置,解决内存溢出问题

    Tomcat默认参数不适合生产环境使用,因此需要修改一些参数 1.修改启动时内存参数.并指定JVM时区 (在Windows Server 2008 下时间少了8个小时): 在Tomcat上运行j2ee ...

  5. Linux下jenkins改端口、解决内存溢出、版本升级

    1.新版本的jenkins修改端口新版本jenkins的配置文件在/etc/sysconfig/jenkinsvi /etc/sysconfig/jenkins找到JENKINS_PORT=" ...

  6. Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题

    最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...

  7. 在Android中解决内存溢出 – OutOfMemoryError

    原文链接:http://riggaroo.co.za/fixing-memory-leaks-in-android-outofmemoryerror/ 注:本文在原文基础上在如何判断内存是否泄露方面进 ...

  8. c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter

    1.XmlTextWriter 表示提供快速.非缓存.只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文 ...

  9. solrCloud设置Tomcat jvm内存解决内存溢出的问题

    几乎已经搜遍了整个网络,没有找到一篇解决设置solr在Tomcat下设置虚拟机内存的文章.   因为之前一直是在Tomcat中设置zkhost参数,在加上jvm参数后会无法启动,添加其他参数也没有生效 ...

随机推荐

  1. 解决CSocket高数据传输问题

    这个是自己项目中发现的问题,所以这个不一定适用于你的. 仅供参考. 头文件: ESSocket.h // ESSocket.h : header file // #ifndef ESSOCKET_H ...

  2. Pywinauto自动化操作PC微信提取好友微信号

    声明:https://zhuanlan.zhihu.com/p/29944988#! /usr/bin/env python #coding=utf-8 #pywinauto自动化操作微信号 #by ...

  3. androidstudio实现增量更新步骤

    本文demo和参考例子参考-传送  门:http://blog.csdn.net/duguang77/article/details/17676797 一.增量更新优点:节省客户端和服务器端流量 增量 ...

  4. Laravel-admin之Driver [] is not supported

    使用Laravel-admin做项目,原本好好的项目,今天一运行则报错:Driver [] is not supported,截图如下: 翻看百度翻译之后,才知道是不支持驱动器[],但是知道意思还是不 ...

  5. python 基础(while 循环、格式化输出、运算符、编码初识)

    while循环 break 终止当前循环 count = 1 while count < 3: print(count) count += 1 break # while循环中一旦代码执行到br ...

  6. Could not parse mapping document from resource com/hs/model/StudentModel.hbm.xml

    网上出现这个问题的 lei.hbm.xml配置写错的,文件头应该改为如下,并不是这个问题 <?xml version="1.0"?> <!DOCTYPE hibe ...

  7. 加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)

    加载Selenium2library失败,检查D:\Python27\Lib\site-packages 目录下是否有Selenium2Library 目录,没有该目录,事情就尴尬了. 自己安装的版本 ...

  8. linux压缩打包

    linux下的压缩命令有tar.gzip.gunzip.bzip2.bunzip2. compress.uncompress.zip.unzip.rar.unrar等等,压缩后的扩展名有.tar..g ...

  9. Leetcode661.Image Smoother图片平滑器

    包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多 ...

  10. PHP的注释规范

    <?php //注释规范 /** *函数的功能 *@param 参数类型 参数名1 参数解析 *@param 参数类型 参数名2 参数解析 *@return 返回值类型 返回值解析 *@auth ...