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"));
相关推荐
okseekw11 分钟前
Java 字符串三巨头:String、StringBuilder、StringJoiner —— 初学者避坑指南 🤯
java
毕设源码余学姐22 分钟前
计算机毕设 java 中医药药材分类采购网站 SSM 框架药材交易平台 Java 开发的分类采购与订单管理系统
java·开发语言·课程设计
BD_Marathon27 分钟前
【JUC】并发与并行
java
okseekw38 分钟前
Java String类详解:不可变性、创建方式与比较方法
java
q***649739 分钟前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
降临-max1 小时前
JavaSE---网络编程
java·开发语言·网络·笔记·学习
带刺的坐椅1 小时前
Solon AI 开发学习5 - chat - 支持哪些模型?及方言定制
java·ai·openai·solon
悟空码字2 小时前
单点登录:一次登录,全网通行
java·后端
傻啦嘿哟2 小时前
物流爬虫实战:某丰快递信息实时追踪技术全解析
java·开发语言·数据库
倚肆2 小时前
Spring Boot Security 全面详解与实战指南
java·spring boot·后端