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

相关推荐
MrSYJ14 分钟前
UserDetailService是在什么环节生效的,为什么自定义之后就能被识别
java·spring boot·后端
long3161 小时前
构建者设计模式 Builder
java·后端·学习·设计模式
吐个泡泡v2 小时前
Maven 核心命令详解:compile、exec:java、package 与 IDE Reload 机制深度解析
java·ide·maven·mvn compile
天上掉下来个程小白2 小时前
微服务-01.导入黑马商城
java·微服务·架构
Noii.2 小时前
Spring Boot初级概念及自动配置原理
java·spring boot·后端
探索java2 小时前
Tomcat Server 组件原理
java·后端·tomcat
勿在浮沙筑高台2 小时前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
用户8356290780512 小时前
Java使用Spire.Doc实现Word转PDF:格式精准的自动化解决方案
java
陆小叁3 小时前
基于Flink CDC实现联系人与标签数据实时同步至ES的实践
java·elasticsearch·flink
CHEN5_023 小时前
【Java基础】反射,注解,异常,Java8新特性,object类-详细介绍
java·开发语言