EasyExcel两行表头

例子:

代码:

c 复制代码
StorageService localStorageService = storageFactory.getLocalStorageService();
        String path = "";
        // 文件信息
        String dateTime = DateUtils.formatTimestampToString(new Date());
        String title = "xxx统计";
        String fileName = StringUtils.dbc2sbcCase(title) + "_" + dateTime + EXCEL_SUFFIX;

        File file = null;
        ExcelWriter excelWriter = null;
        WriteSheet writeSheet = null;
        int num = 0;
        try {
            file = localStorageService.newTempFile(fileName);
            excelWriter = EasyExcel.write(file.getPath()).build();

            writeSheet = EasyExcel.writerSheet(title).sheetNo(0).registerWriteHandler(new CustomizeColumnWidth()).build();

            // 写入数据
            List<List<String>> headList = new ArrayList<>();
            headList.add(Lists.newArrayList(title,"数据1"));
            headList.add(Lists.newArrayList(title,"数据2"));
            headList.add(Lists.newArrayList(title,"数据3"));
            //数据
            List<List<String>> objects = new ArrayList<>();
            objects.add(Lists.newArrayList("123","321","222"));

            WriteTable writeTable = EasyExcel.writerTable(num)
                    .head(headList)
                    .registerWriteHandler(ExcelUtils.getStyleStrategy()).build();
            excelWriter.write(Lists.newArrayList(objects), writeSheet, writeTable);
            num++;

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (excelWriter != null) {
            // 写入数据
            List<List<String>> headList = new ArrayList<>();
            String tableTitle = "第二个表题";
            headList.add(Lists.newArrayList(tableTitle,"姓名"));
            headList.add(Lists.newArrayList(tableTitle,"年龄"));
            headList.add(Lists.newArrayList(tableTitle,"性别"));
            //数据
            List<List<String>> objects = new ArrayList<>();
            objects.add(Lists.newArrayList("admin","18","男"));
            objects.add(Lists.newArrayList("admin2","19","男"));

            WriteTable writeTable = EasyExcel.writerTable(num)
                    .head(headList)
                    .registerWriteHandler(ExcelUtils.getStyleStrategy()).build();
            excelWriter.write(Lists.newArrayList(objects), writeSheet, writeTable);
            num++;
            try {
                excelWriter.finish();
                path = FileUtils.uploadFile(file, fileName, orgId, userId);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 生成的文件的路径
        return path;

重点:

需要合并列的字段重复设到表头中

c 复制代码
List<List<String>> headList = new ArrayList<>();
            headList.add(Lists.newArrayList(title,"数据1"));
            headList.add(Lists.newArrayList(title,"数据2"));
            headList.add(Lists.newArrayList(title,"数据3"));
相关推荐
阿伟*rui26 分钟前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
paopaokaka_luck2 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
dayouziei2 小时前
java的类加载机制的学习
java·学习
Yaml44 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~4 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616884 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
aloha_7895 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
记录成长java5 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
睡觉谁叫~~~5 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust