前言

java大数据量导出excel,谨以此篇博客和大家分享下。

需求

需要导出几十万甚至几百万数据到excel。

实现

HouseInfoVO.java

@Data
public class HouseInfoVO {
	@Excel(name = "id")
    private Long id;
    @Excel(name = "编码")
    private String code;
}

关键代码:

List<HouseInfoVO> houseInfoVOS = houseInfoService.test(houseInfoQueryDTO);
Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams(null, "1"), HouseInfoVO.class, houseInfoVOS);

ExportParams两个参数:
第一个:标题行名称(设为null代表没有标题行,有的话会在excel第一行展示)
第二个:sheet名称

调用excel导出浏览器方法

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 ServiceException(e.getMessage());
    }
}

上面提供了 EasyPoi的exportBigExcel导出大数据量方法,我试过16w数据导出只要10s左右,如果你导出时间过长可能是你查询数据库耗时过长,需要sql优化了,有时间的话可以研究下阿里的EasyExcel

这里提供另一种思路:如果数据量过大,可以采取分sheet页导出,比如每个sheet导十万导多个sheet页

实现代码:

try {
           
     List<Map<String,Object>> sheets = new ArrayList<Map<String,Object>>();
       
     for(int i = 0;i<10;i++) {
         Map<String,Object> sheet = new HashMap<String,Object>();
         	// 外卖满减红包、饿了么红包、淘票票电影券、支付宝消费红包关注下方公众号
         	// 干饭必备外卖神券
            List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();//构造对象等同于@Excel
            
            entity.add(new ExcelExportEntity("性别", "sex"));
            entity.add(new ExcelExportEntity("姓名", "name"));
            
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            Map<String,Object> h1 = new HashMap<String,Object>();
            h1.put("name", "name" + i);
            h1.put("sex", "sex" +  i);
            Map<String,Object> h2 = new HashMap<String,Object>();
            h2.put("name", "name" +i+i);
            h2.put("sex", "sex" +i+i);
            list.add(h1);
            list.add(h2);
            
            sheet.put(NormalExcelConstants.CLASS, ExcelExportEntity.class);
            sheet.put(NormalExcelConstants.DATA_LIST, list);
            sheet.put(NormalExcelConstants.PARAMS, new ExportParams(null, "sheet"+i));
            sheet.put(NormalExcelConstants.MAP_LIST, entity);
            sheets.add(sheet);
        }
        
      Workbook workbook = new HSSFWorkbook();
      
         for(Map<String,Object> map : sheets) {
             ExcelExportService server = new ExcelExportService();
            ExportParams param = (ExportParams) map.get("params");
           @SuppressWarnings("unchecked")
List<ExcelExportEntity> entity = (List<ExcelExportEntity>) map.get("mapList");
             Collection<?> data = (Collection<?>) map.get("data");
            server.createSheetForMap(workbook, param, entity, data);
         }

        FileOutputStream fos = new FileOutputStream("D:/ExcelExportForMap.tt.xls");
        workbook.write(fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();

在这里插入图片描述

写在最后

非常感谢大家的认真阅读,如果有其他好代码技巧都可以和我交流哦,如有不足,还望各位看官多批评指正=_=
技术交流秋秋群:719023986

微x关注:干饭必备外卖神券,每天领大额卷
微x关注:正好想买,自助查桃宝京d卷

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐