美文网首页全栈程序猿的成长Java学习笔记程序员
【springboot+easypoi】大数据量excel导出

【springboot+easypoi】大数据量excel导出

作者: 小尘哥 | 来源:发表于2018-02-08 11:41 被阅读719次

上次写了一行代码解决导出导入,没看的小伙伴建议先看下《【springboot+easypoi】一行代码搞定excel导入导出》,但是实际业务中遇到一个问题,如果数据里比较大的时候,例如10w+数据一次导出,就会出现卡死情况,继续看官方文档,有大数据量导出方法,实现如下


 @RequestMapping("export")
    public void export(HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        Workbook workbook = bigExcel(1, params, null, new ExportParams("海贼王", "海贼王"), new Page<>());
        ExcelExportUtil.closeExportBigExcel();
        downLoadExcel("海贼王.xls", response, workbook);
    }


    private Workbook bigExcel(int pageNum, Map<String, Object> params, Workbook workbook, ExportParams exportParams, Page<SysUser> page) {
        //分页查询数据
        page.setCurrent(pageNum);
        page.setSize(1000);
        page.setCondition(params);
        page = this.getData(sysUserService.queryPage(page));
        List<SysUser> users = FastJsonUtils.toList(FastJsonUtils.toJSONString(page.getRecords()), SysUser.class);

        workbook = ExcelExportUtil.exportBigExcel(exportParams, SysUser.class, users);

      //如果不是最后一页,递归查询
        if (page.getPages() > pageNum) {
            bigExcel(pageNum + 1, params, workbook, exportParams, page);
        }
        return workbook;
    }

    private void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("content-Type", "application/vnd.ms-excel");
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new NormalException(e.getMessage());
        }
    }

备注:导出11w+数据大概半分钟左右,sql以及导出数据处理都可以再优化,其他的就看各位小伙伴的需求了

相关文章

网友评论

  • ayinmu:能把完整的代码发给我吗?
    小尘哥:@ayinmu 开源的,欢迎使用
    ayinmu:@小尘哥 能直接用吗?
    小尘哥:@ayinmu https://gitee.com/QuanZhanZhiLu/easy-boot这个是整个项目的源码,谢谢点star哦
  • 5de1c4f35e2c:请问 this.getData(sysUserService.queryPage(page)) 这一步就是 先单独根据查询条件查询总共需要分几页的页数吧。另外如果需要返回给前端导出的进度,可以在bigExcel()方法这里返回出去
  • b51067229d87:为什么不关闭输出流?:flushed:

本文标题:【springboot+easypoi】大数据量excel导出

本文链接:https://www.haomeiwen.com/subject/cdxttftx.html