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

December 17, 2023
测试
测试
测试
测试
3 分钟阅读

01

上次写了一行代码解决导出导入,没看的小伙伴建议先看下《一行代码做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());
        }
}

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多