大数据量的写入
HSSF(03版)
实战
抛出异常测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class ExcelBigData { private static String PATH = "/Users/panyurou/IdeaProjects/exceldemo";
@Test public void excelWrite03() throws IOException { Workbook workbook = new HSSFWorkbook(); final Sheet sheet = workbook.createSheet("测试大数据量"); final long begin = System.currentTimeMillis(); for (int i = 0; i < 65537; i++) { final Row row = sheet.createRow(i); for (int j = 0; j < 10; j++) { final Cell cell = row.createCell(j); cell.setCellValue("${i,j}"); } } System.out.println("over!"); FileOutputStream outputStream = new FileOutputStream(PATH + "/excel大数据写入01.xls"); workbook.write(outputStream); outputStream.close(); System.out.println("excel 写入完毕!"); final long end = System.currentTimeMillis(); System.out.println("写入时间:"+(double) ((end - begin) / 1000)); } }
|

将上面的65537改成65536,输出写入时长,可看出花费了1秒

SXSSFWorkbook
- 优点:可以写非常大的数据量,写速度更快,占用更少的内存
- 注意:
- 过程中会产生临时文件,需要清理临时文件
- 默认100条数据会被保留在内存中,如果超过这数量,则数据会被写入临时文件
- 如果想自定义内存中的数量,可以使用
new SXSSFWorkbook(数量);
实战
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public void excelWriteSXSSF() throws IOException { SXSSFWorkbook workbook = new SXSSFWorkbook(); final Sheet sheet = workbook.createSheet("测试大数据量"); final long begin = System.currentTimeMillis(); for (int i = 0; i < 65537; i++) { final Row row = sheet.createRow(i); for (int j = 0; j < 120; j++) { final Cell cell = row.createCell(j); cell.setCellValue("${i,j}"); } } System.out.println("over!"); FileOutputStream outputStream = new FileOutputStream(PATH + "/excel大数据写入09s.xlsx"); workbook.write(outputStream); outputStream.close(); workbook.dispose(); System.out.println("excel 写入完毕!"); final long end = System.currentTimeMillis(); System.out.println("写入时间:"+(double) ((end - begin) / 1000)); }
|
可以看出即使现在有120列,也只需要10秒,速度很快。
