在poi3.8之后为解决内存溢出,针对excel2007提出一个新类SXSSFWorkbook

package com.mjs.bigdata.antifraud.controller;

import java.io.FileOutputStream;

import org.apache.commons.lang.exception.ExceptionUtils;

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;

public class Test3 {

public static void main(String[] args) {

sxssfTest();

}

/**

* 在poi3.8之后为解决内存溢出,针对excel2007提出一个新类SXSSFWorkbook

*/

public static void sxssfTest() {

try {

/**

* new SXSSFWorkbook(100)

* 这里SXSSFWorkbook类采用一边读取一边写的方式,每次写完就释放前者所创建的row对象

* 100则是表示每次读取和创建的row的个数

*/

Workbook wb = new SXSSFWorkbook(100);

Sheet sh = wb.createSheet();

int rownum = 0;

while (true) {

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);

}

System.out.println(rownum);

rownum++;

if (rownum >= 200000)

break;

}

FileOutputStream out = new FileOutputeam(Str"d:/sxssf.xlsx");

wb.write(out);

out.close();

} catch (Exception e) {

System.out.println(ExceptionUtils.getFullStackTrace(e));

}

}

}

猜你喜欢

转载自blog.csdn.net/chinaxiaofeng8/article/details/82426278