首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

POI3.8组件研究(8)-基于SXSSF (Streaming Usermodel API)的写文件

2012-07-30 
POI3.8组件研究(八)--基于SXSSF (Streaming Usermodel API)的写文件package com.easyway.excel.events.str

POI3.8组件研究(八)--基于SXSSF (Streaming Usermodel API)的写文件
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

热点排行