+ -
当前位置:首页 → 问答吧 → POI 产生巨大 EXCEL 报表

POI 产生巨大 EXCEL 报表

时间:2011-08-28

来源:互联网

POI 产生巨大 EXCEL 报表

最近看 POI 的官网发现在产生 EXCEL 的部分有加入些新的东西:
1
2
3
4
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


再看看范例程式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import junit.framework.Assert;
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 static void main(String[] args) throws Throwable { Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk Sheet sh = wb.createSheet(); 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 for(int rownum = 0; rownum < 900; rownum++){ Assert.assertNull(sh.getRow(rownum)); }
  // ther last 100 rows are still in memory for(int rownum = 900; rownum < 1000; rownum++){ Assert.assertNotNull(sh.getRow(rownum)); } FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx"); wb.write(out); out.close(); }


其中的吸引我的就在 SXSSFWorkbook 能设定要保存多少 rows 在记忆体中处理
1
Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk


这表示只要利用这个新的 API,我们就能在不耗费太多记忆体的情况下产生巨大的 EXCEL 档

作者: qrtt1   发布时间: 2011-08-28

Hi,不知道您有无遇过用此方式,在不同的sheet产生cell写入资料时,遇到问题?
我用此方式在多个sheet时,会遇到第2个以后的sheet,资料并没有写入.
不知道这是不是此API的限制?

作者: stevewan   发布时间: 2011-09-14

热门下载

更多