package com.java.test;
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;
/**
* 解决POI导出Excel文件内存溢出问题
* @author pyi
*/
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 >= 10000)
break;
}
FileOutputStream out = new FileOutputStream("E:/temp/sxssf.xlsx");
wb.write(out);
out.close();
} catch (Exception e) {
System.out.println(ExceptionUtils.getFullStackTrace(e));
}
}
}
解决POI导出Excel文件内存溢出问题
猜你喜欢
转载自blog.csdn.net/qq844579582/article/details/53084753
今日推荐
周排行