EasyExcel两行表头

news/2024/7/21 7:37:28 标签: java, excel

例子:
在这里插入图片描述
代码:

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;

重点:

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

List<List<String>> headList = new ArrayList<>();
            headList.add(Lists.newArrayList(title,"数据1"));
            headList.add(Lists.newArrayList(title,"数据2"));
            headList.add(Lists.newArrayList(title,"数据3"));

http://www.niftyadmin.cn/n/5231356.html

相关文章

14、深度学习之推理和训练

推理和训练是深度学习中两个非常重要的概念。很多初学者可能不太清楚,但是很好区分也很好理解。 如果我们把一个模型比作一个人的话,那么训练过程就相当于初中、高中的学习, 而推理过程相当于去参加高考。 初中高中阶段,我们通过学习大量的知识来训练自己的大脑。在去参加高…

Day62.算法训练

718. 最长重复子数组 class Solution {public int findLength(int[] nums1, int[] nums2) {int max 0;int[][] dp new int[nums1.length][nums2.length];for (int i 0; i < nums1.length; i) {for (int j 0; j < nums2.length; j) {if (nums1[i] nums2[j]) {if (i …

微前端个人理解与简单总结

最近一段时间在学习微前端&#xff0c;一开始是看各种博客了解微前端含义、对比多种微前端框架优劣&#xff0c;最后选择了qiankun、micro-app、wujie这三种微前端框架进行深入研究、对比。 微前端框架 推出时间 官方文档易读性 社区讨论活跃度 配置难度 Qiankun&#xff…

vue请求如何按顺序执行

我们有时候会碰到这种情况&#xff0c;需要连续发送两个请求&#xff0c;第二个请求需要用第一个请求的某个返回值作为参数来作为第二个请求的请求参数。 但是存在一个问题&#xff1a;两个请求都是异步的&#xff0c;他并不按照我们期望的先后顺序来执行。 这时候就需要控制请…

聊聊springboot的http.server.requests

序 本文主要研究一下springboot的http.server.requests http.server.requests org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java public static class Server {private final ServerRequest request new ServerRequest();/*** Maximum numb…

AI语音机器人可以为企业提供什么工作效率?ai机器人源码

AI语音机器人是一种基于人工智能技术的语音交互系统&#xff0c;能够通过自然语言理解和语音合成技术实现与用户的智能对话。AI语音机器人可以为企业提供以下方面的帮助&#xff0c;从而提高工作效率&#xff1a; 自动客服&#xff1a;AI语音机器人可以代替人工客服完成一些简单…

申请开通QMT量化需要多少资金?免费开通!

最近量化交易在市场上大火&#xff0c;很多投资者想要参与进来。QMT量化软件是目前市场上一款比较常见并且强大的量化软件。那开通QMT量化交易软件需要多少资金&#xff1f; QMT量化交易软件是一种专门用于量化交易的工具&#xff0c;它能够帮助投资者通过程序化交易策略进行股…

Linux体系架构----Linux根目录下常见一级子目录的作用

文章目录 Linux 根目录下的一级子目录扮演着重要的角色&#xff0c;每个子目录都有其特定的作用和功能。以下是常见的 Linux 根目录下一级子目录及其作用&#xff1a; /bin&#xff08;Binary&#xff09;&#xff1a; 作用&#xff1a;存放系统启动和恢复所需的基本命令&#…