天天看點

easypoi導出excel 效率_【springboot+easypoi】大資料量excel導出

上次寫了一行代碼解決導出導入,沒看的小夥伴建議先看下《【springboot+easypoi】一行代碼搞定excel導入導出》,但是實際業務中遇到一個問題,如果資料裡比較大的時候,例如10w+資料一次導出,就會出現卡死情況,繼續看官方文檔,有大資料量導出方法,實作如下

@RequestMapping("export")

public void export(HttpServletResponse response) {

Map 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 params, Workbook workbook, ExportParams exportParams, Page page) {

//分頁查詢資料

page.setCurrent(pageNum);

page.setSize(1000);

page.setCondition(params);

page = this.getData(sysUserService.queryPage(page));

List 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以及導出資料處理都可以再優化,其他的就看各位小夥伴的需求了