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

相关推荐
chools1 分钟前
Java后端拥抱AI开发之个人学习路线 - - Spring AI【第一期】
java·人工智能·学习·spring·ai
jeCA EURG19 分钟前
Spring Boot 2.7.x 至 2.7.18 及更旧的版本,漏洞说明
java·spring boot·后端
BduL OWED1 小时前
Redis之Redis事务
java·数据库·redis
FastBean1 小时前
BizAssert:一个轻量级、生产就绪的 Java 业务断言工具类
java·后端
zhuiyisuifeng1 小时前
Node.js使用教程
java
李庆政3701 小时前
Reactor-core 响应式编程 spring-boot-starter-webflux
java·spring boot·reactor·响应式编程·reactor-core
是Smoky呢1 小时前
springAI+向量数据库+RAG入门案例
java·开发语言·ai编程
huabiangaozhi1 小时前
修改表字段属性,SQL总结
java·数据库·sql
请为小H留灯1 小时前
一键解决 IDEA 中 Java 项目变橙色的问题!!!
java·ide·maven·intellij-idea·java项目
小文大数据2 小时前
python实现HTML转PDF
java·前端·数据库