poi导出百万数据到excel,只在瞬息之间

public void exportBigDataExcel(DTO dto, String path)
            throws IOException {
        // 最重要的就是使用SXSSFWorkbook,表示流的方式进行操作
        // 在内存中保持100行,超过100行将被刷新到磁盘
        SXSSFWorkbook wb = new SXSSFWorkbook(100);
        Sheet sh = wb.createSheet(); // 建立新的sheet对象
        Row row = sh.createRow(0);   // 创建第一行对象
        // -----------定义表头-----------
        Cell cel0 = row.createCell(0);
        cel0.setCellValue("xxx0");
        Cell cel1 = row.createCell(1);
        cel1.setCellValue("xxx1");
        Cell cel2 = row.createCell(2);
        cel2.setCellValue("xxx2");
        Cell cel3 = row.createCell(3);
        cel3.setCellValue("xxx3");
        Cell cel4 = row.createCell(4);
        cel4.setCellValue("xxx4");
        Cell cel5 = row.createCell(5);
        cel5.setCellValue("xxx5");
        Cell cel6 = row.createCell(6);
        cel6.setCellValue("xxx6");
        Cell cel7 = row.createCell(6);
        cel7.setCellValue("xxx7");
        // ---------------------------
        List<DTO> list = new ArrayList<DTO>();
        // 数据库中存储的数据行
        int page_size = 10000;
        // 求数据库中待导出数据的行数
        int list_count = (int) dao.getListTotal(dto);
        // 根据行数求数据提取次数
        int export_times = list_count % page_size > 0 ? list_count / page_size
                + 1 : list_count / page_size;
        if(list_count<page_size) {
            page_size=list_count;
        }
        // 按次数将数据写入文件
        for (int j = 0; j < export_times; j++) {
            //分页
            dto.setStart(page_size*j+1);
            dto.setEnd(page_size*(j+1));
            list=dao.getList(dto);

            int len = list.size() < page_size ? list.size() : page_size;
            for (int i = 0; i < len; i++) {
                Row row_value = sh.createRow(j * page_size + i + 1);
                Cell cel0_value = row_value.createCell(0);
                cel0_value.setCellValue(list.get(i).getxx0());
                Cell cel1_value = row_value.createCell(1);
                cel1_value.setCellValue(list.get(i).getxx1());
                Cell cel2_value = row_value.createCell(2);
                cel2_value.setCellValue(list.get(i).getxx2());
                Cell cel3_value = row_value.createCell(3);
                cel3_value.setCellValue(list.get(i).getxx3());
                Cell cel4_value = row_value.createCell(4);
                cel4_value.setCellValue(list.get(i).getxx4());
                Cell cel5_value = row_value.createCell(5);
                cel5_value.setCellValue(list.get(i).getxx5());
                Cell cel6_value = row_value.createCell(6);
                cel6_value.setCellValue(list.get(i).getxx6());
                Cell cel7_value = row_value.createCell(7);
                cel7_value.setCellValue(list.get(i).getxx7());
            }
            list.clear(); // 每次存储len行,用完了将内容清空,以便内存可重复利用
        }
        FileOutputStream fileOut = new FileOutputStream(path);
        wb.write(fileOut);
        fileOut.close();
        wb.dispose();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值