查看: 537|回复: 0

[Java学习] 使用SXSSFWorkbook来导出excel

发表于 2017-8-8 08:00:03
尚学堂AD

SXSSFWorkbook是XSSFWorkbook的Streaming版本,实现了"BigGridDemo"的策略,在导出大量数据的时候,可以避免OOM。

使用实例
  1. @Test
  2. public void testWriteLargeData() throws IOException {
  3. FileInputStream inputStream = new FileInputStream("mytemplate.xlsx");
  4. XSSFWorkbook wb_template = new XSSFWorkbook(inputStream);
  5. inputStream.close();
  6. SXSSFWorkbook wb = new SXSSFWorkbook(wb_template);
  7. wb.setCompressTempFiles(true);
  8. SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0);
  9. sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk
  10. for(int rownum = 4; rownum < 100000; rownum++){
  11. Row row = sh.createRow(rownum);
  12. for(int cellnum = 0; cellnum < 10; cellnum++){
  13. Cell cell = row.createCell(cellnum);
  14. String address = new CellReference(cell).formatAsString();
  15. cell.setCellValue(address);
  16. }
  17. }
  18. FileOutputStream out = new FileOutputStream("tempsxssf.xlsx");
  19. wb.write(out);
  20. out.close();
  21. // dispose of temporary files backing this workbook on disk
  22. wb.dispose();
  23. }
复制代码
doc BigGridDemo


回复

使用道具 举报