您的当前位置:首页正文

EasyExcel动态实现表头以及数据封装

2024-11-29 来源:个人技术集锦

EasyExcel动态实现表头以及数据封装

实现方法

  public void exportSelfRanking(HttpServletResponse response, ButtonStatisticsParam buttonStatisticsParam) throws IOException {
        //表头参数
        List<List<String>> titleList = queryButtonExportTitle();
        //数据
        List<List<String>> dataList = queryButtonExportData(buttonStatisticsParam, titleList);
        //导出
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("使用情况数据"+DateUtil.format(new Date(),"yyyyMMddHHmmssSSS"), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream())
                .head(titleList)
                .registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
                .sheet("功能意向使用情况").doWrite(dataList);
    }

注:buttonStatisticsParam为导出的检索条件,response为请求头参数
List<List> titleList 为封装的动态表头参数, List<List> dataList 为动态封装的数据参数,registerWriteHandler(new SimpleColumnWidthStyleStrategy(25)) 为设置表格参数,head(titleList) 为存放表头参数

显示全文