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

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) 为存放表头参数

相关推荐
特立独行的猫a1 分钟前
11款常用C++在线编译与运行平台推荐与对比
java·开发语言·c++
louisgeek11 分钟前
Java 位运算
java
hweiyu001 小时前
Maven 私库
java·maven
Super Rookie1 小时前
Spring Boot 企业项目技术选型
java·spring boot·后端
写不出来就跑路1 小时前
Spring Security架构与实战全解析
java·spring·架构
ZeroNews内网穿透2 小时前
服装零售企业跨区域运营难题破解方案
java·大数据·运维·服务器·数据库·tcp/ip·零售
sleepcattt2 小时前
Spring中Bean的实例化(xml)
xml·java·spring
lzzy_lx_20892 小时前
Spring Boot登录认证实现学习心得:从皮肤信息系统项目中学到的经验
java·spring boot·后端
Dcs2 小时前
立即卸载这些插件,别让它们偷你的资产!
java
小七mod2 小时前
【Spring】Java SPI机制及Spring Boot使用实例
java·spring boot·spring·spi·双亲委派