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"));
相关推荐
2202_754421549 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
蓝染-惣右介12 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
小林想被监督学习13 分钟前
idea怎么打开两个窗口,运行两个项目
java·ide·intellij-idea
HoneyMoose15 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热16 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余17 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp18 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang20 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
一直学习永不止步26 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
hummhumm40 分钟前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j