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

相关推荐
咸鱼2.01 小时前
【java入门到放弃】跨域
java·开发语言
indexsunny1 小时前
互联网大厂Java求职面试实战:微服务与Spring生态全攻略
java·数据库·spring boot·安全·微服务·面试·消息队列
沐苏瑶1 小时前
Java 搜索型数据结构全解:二叉搜索树、Map/Set 体系与哈希表
java·数据结构·算法
冬夜戏雪2 小时前
实习面经记录(十)
java·前端·javascript
skiy2 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
平生不喜凡桃李2 小时前
浅谈 Linux 中 namespace 相关系统调用
java·linux·服务器
zb200641202 小时前
CVE-2024-38819:Spring 框架路径遍历 PoC 漏洞复现
java·后端·spring
2401_895521342 小时前
spring-ai 下载不了依赖spring-ai-openai-spring-boot-starter
java·人工智能·spring
何仙鸟3 小时前
GarmageSet下载和处理
java·开发语言
wefly20173 小时前
免安装!m3u8live.cn在线 M3U8 播放器,小白也能快速上手
java·开发语言·python·json·php·m3u8·m3u8在线转换