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"));
相关推荐
TT哇几秒前
*【每日一题 提高题】[蓝桥杯 2022 国 A] 选素数
java·算法·蓝桥杯
火烧屁屁啦23 分钟前
【JavaEE进阶】初始Spring Web MVC
java·spring·java-ee
w_312345437 分钟前
自定义一个maven骨架 | 最佳实践
java·maven·intellij-idea
岁岁岁平安39 分钟前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA42 分钟前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
Q_19284999061 小时前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
张国荣家的弟弟1 小时前
【Yonghong 企业日常问题 06】上传的文件不在白名单,修改allow.jar.digest属性添加允许上传的文件SH256值?
java·jar·bi
ZSYP-S1 小时前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
yuanbenshidiaos1 小时前
C++----------函数的调用机制
java·c++·算法
是小崔啊2 小时前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴