easyExcel - 动态复杂表头的编写

news2024/10/6 0:37:23

目录

  • 前言
  • 一、情景介绍
  • 二、问题分析
  • 三、代码实现
    • 方式一:head 设置
    • 方式二:模板导出
    • 方式三:自定义工具类


前言

Java-easyExcel入门教程:https://blog.csdn.net/xhmico/article/details/134714025

之前有介绍过如何使用 easyExcel,以及写了两个入门的 demo ,这两个 demo 能应付在开发中大多数的导入和导出需求,不过有时候面对一些复杂的表格,就会有点不够用,该篇讲述的是如何实现复杂表头编写


一、情景介绍

在实际的开发过程中可能会遇到需要导出一些带有复杂表头的表格,比如以下案例:

在这里插入图片描述

该表头占了两行,其中 橙色 部分的信息是需要动态生成的


二、问题分析

关于如何实现类似于上述复杂表头,有多种方式均可实现,首先这个表头是一个复杂表头,其次还有动态的部分

查看官方文档,对应复杂表头的实现

官方文档:复杂头写入

在这里插入图片描述

从中可以看出,多行表头就是由 多个纵向 的列表组成,并且表头 相同的部分会自动合并居中对齐

再查阅官方文档关于如何实现动态头的写入

官方文档:动态头、实时生成头写入

在这里插入图片描述

官方给了一个 head() 方法允许我们在代码中自定义表头

    public T head(List<List<String>> head) {
        this.parameter().setHead(head);
        return this.self();
    }

三、代码实现


方式一:head 设置

可以将上述表头看作是以下 6 个集合组成的表头,然后使用 head() 方法去设置

在这里插入图片描述

代码示例:

    /**
     * 复杂表头编写:方式一
     */
    @Test
    public void complexHeadDemo01() {
        // 输出文件路径
        String outFilePath = "D:\\excel-files\\demo01.xlsx";

        // 表格数据
        List<Object> data = new ArrayList<>();

        EasyExcel.write(outFilePath)
                // 动态头
                .head(head())
                .sheet()
                // 表格数据
                .doWrite(data);
    }

    private List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("部门");
        head0.add("用户名称");
        List<String> head1 = new ArrayList<String>();
        head1.add("运营部");
        head1.add("性别");
        List<String> head2 = new ArrayList<String>();
        head2.add("运营部");
        head2.add("年龄");
        List<String> head3 = new ArrayList<String>();
        head3.add("时间");
        head3.add("出生日期");
        List<String> head4 = new ArrayList<String>();
        head4.add("2024-04-09");
        head4.add("学历");
        List<String> head5 = new ArrayList<String>();
        head5.add("2024-04-09");
        head5.add("电话号码");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        return list;
    }

结果展示:

在这里插入图片描述

可以看到是能够实现这种表头的,不过需要自己定义表头的样式


方式二:模板导出

可以使用模板导出的方式,设置一个模板文件,例如:

在这里插入图片描述

实体类:
DeptUserExcelEntity.java

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DeptUserExcelEntity {

    @ApiModelProperty(value = "用户名称")
    private String realName;

    @ApiModelProperty(value = "性别")
    private String gender;

    @ApiModelProperty(value = "年龄")
    private Integer age;

    @ApiModelProperty(value = "出生日期")
    private String birthdate;

    @ApiModelProperty(value = "学历")
    private String education;

    @ApiModelProperty(value = "电话号码")
    private String telephone;
}

代码示例:

    /**
     * 复杂表头编写:方式二
     */
    @Test
    public void complexHeadDemo02() {
        // 模板文件路径
        String templateFilePath = "D:\\excel-files\\template.xlsx";
        // 输出文件路径
        String outFilePath = "D:\\excel-files\\demo02.xlsx";

        // 创建 ExcelWriter 实例
        ExcelWriter writer = EasyExcel
                // 写入到
                .write(outFilePath)
                // 指定模板
                .withTemplate(templateFilePath)
                .build();

        WriteSheet sheet = EasyExcel.writerSheet().build();

        Map<String, String> replaceMap = new HashMap<>();
        replaceMap.put("deptName", "运营部");
        replaceMap.put("currentDate", "2024-04-09");

        // 执行填充普通占位符操作
        writer.fill(replaceMap, sheet);

        // 获取员工信息
        List<DeptUserExcelEntity> data = new ArrayList<>();

        FillConfig fillConfig = FillConfig.builder()
                // 开启填充换行
                .forceNewRow(true)
                .build();

        // 执行填充列表操作
        writer.fill(data, fillConfig, sheet);

        // 结束
        writer.finish();
    }

结果展示:

在这里插入图片描述

可以看到效果是比较好的,也不用担心表格样式的问题

关于如何使用 easyexcel 实现按模板导出,可参考:easyExcel - 按模板导出 有较为详细的说明


方式三:自定义工具类

根据官方实现复杂表头的写法,自定义输出对象为

DeptUserExcelEntity.java

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
// 头背景设置
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, horizontalAlignment = HorizontalAlignmentEnum.CENTER, borderLeft = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
//标题高度
@HeadRowHeight(40)
//内容高度
@ContentRowHeight(30)
//内容居中,左、上、右、下的边框显示
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, borderLeft = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
public class DeptUserExcelEntity {

    @ApiModelProperty(value = "用户名称")
    @ExcelProperty({"部门","用户名称"})
    @ColumnWidth(15)
    private String realName;

    @ApiModelProperty(value = "性别")
    @ExcelProperty({"deptName","性别"})
    @ColumnWidth(15)
    private String gender;

    @ApiModelProperty(value = "年龄")
    @ExcelProperty({"deptName","年龄"})
    @ColumnWidth(15)
    private Integer age;

    @ApiModelProperty(value = "出生日期")
    @ExcelProperty({"时间","出生日期"})
    @ColumnWidth(15)
    private String birthdate;

    @ApiModelProperty(value = "学历")
    @ExcelProperty({"currentDate","学历"})
    @ColumnWidth(20)
    private String education;

    @ApiModelProperty(value = "电话号码")
    @ExcelProperty({"currentDate","电话号码"})
    @ColumnWidth(20)
    private String telephone;
}

如果依照之前的导出案例,代码如下:

    @Test
    public void complexHeadDemo03_test() {

        // 输出文件路径
        String outFilePath = "D:\\excel-files\\demo03.xlsx";

        List<DeptUserExcelEntity> excelEntities = new ArrayList<>();
        EasyExcel.write(outFilePath, DeptUserExcelEntity.class)
                .sheet()
                .doWrite(excelEntities);
    }

最后得到的表格也是根据输出对象定义而来的

在这里插入图片描述

所以如果能让表格在写入的时候,输出对象 DeptUserExcelEntity@ExcelProperty 里面的 deptNamecurrentDate 替换成想要的不就行了,所以我就自定义一个工具类,在需要的时候改变注解的属性值就行了

工具类:

AnnotationUtils.java

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * 注解工具类
 */
public class AnnotationUtils {

    /**
     * 变更注解的属性值再处理业务,处理完业务之后恢复类的属性
     *
     * @param clazz     注解所在的实体类
     * @param tClass    注解类
     * @param attrName 要修改的注解属性名
     * @param attrTypeEnum 要修改的注解属性的类型
     * @param valueMap  要设置的属性值
     */
    public static <A extends Annotation> void changeAnnotationValueToDealProcess(
            Class<?> clazz,
            Class<A> tClass,
            String attrName,
            AttrTypeEnum attrTypeEnum,
            Map<String, String> valueMap,
            DealProcess dealProcess) {
        try {

            Map<String, Object> fieldAnnotationValueMap = new HashMap<>();

            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                A annotation = field.getAnnotation(tClass);
                if (annotation == null) continue;
                Object value = setAnnotationValue(annotation, attrName, attrTypeEnum, valueMap);
                String fieldName = field.getName();
                fieldAnnotationValueMap.put(fieldName, value);
            }

            // 处理业务逻辑
            dealProcess.deal();

            // 恢复
            for (Field field : fields) {
                A annotation = field.getAnnotation(tClass);
                String fieldName = field.getName();
                if (annotation == null) continue;
                Object value = fieldAnnotationValueMap.get(fieldName);

                InvocationHandler handler = Proxy.getInvocationHandler(annotation);
                Field memberValuesField = handler.getClass().getDeclaredField("memberValues");
                memberValuesField.setAccessible(true);
                @SuppressWarnings("all")
                Map<String, Object> memberValues = (Map) memberValuesField.get(handler);
                memberValues.put(attrName, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置注解中的字段值
     *
     * @param annotation 要修改的注解实例
     * @param attrName  要修改的注解属性名
     * @param attrTypeEnum 要修改的注解属性的类型
     * @param valueMap   替换属性集的map
     */
    @SuppressWarnings("all")
    private static Object setAnnotationValue(Annotation annotation, String attrName,
                                             AttrTypeEnum attrTypeEnum, Map<String, String> valueMap) throws NoSuchFieldException, IllegalAccessException {
        InvocationHandler handler = Proxy.getInvocationHandler(annotation);
        Field field = handler.getClass().getDeclaredField("memberValues");
        field.setAccessible(true);
        Map memberValues = (Map) field.get(handler);
        Object value = memberValues.get(attrName);
        switch (attrTypeEnum) {
            case STRING: {
                String oldValue = (String) value;
                String newValue = valueMap.get(oldValue);
                if (StringUtils.isNotBlank(newValue)) {
                    memberValues.put(attrName, newValue);
                }
            }
            break;
            case STRING_ARR: {
                String[] oldValue = (String[]) value;
                String[] newValue = new String[oldValue.length];
                for (int i = 0; i < oldValue.length; i++) {
                    String replace = valueMap.get(oldValue[i]);
                    newValue[i] = replace != null ? replace : oldValue[i];
                }
                memberValues.put(attrName, newValue);
            }
            break;
        }
        return value;
    }

    public enum AttrTypeEnum {
        STRING,
        STRING_ARR
    }

    public interface DealProcess {

        void deal() throws Exception;

    }

}

代码示例:

    /**
     * 复杂表头编写:方式三
     */
    @Test
    public synchronized void complexHeadDemo03() {

        // 输出文件路径
        String outFilePath = "D:\\excel-files\\demo03.xlsx";

        // 替换注解中的属性值为
        HashMap<String, String> replaceMap = new HashMap<>();
        replaceMap.put("deptName", "运营部");
        replaceMap.put("currentDate", "2024-04-09");

        /*
         * 这里简单的说明一下:
         *      attrName: 对应的是 @ExcelProperty 中的 value 属性
         *      attrTypeEnum: 对应的是 @ExcelProperty 中的 value 属性 的类型
         */
        AnnotationUtils.changeAnnotationValueToDealProcess(
                DeptUserExcelEntity.class, ExcelProperty.class, "value", AnnotationUtils.AttrTypeEnum.STRING_ARR, replaceMap, new AnnotationUtils.DealProcess() {
                    @Override
                    public void deal() {
                        List<DeptUserExcelEntity> excelEntities = new ArrayList<>();
                        EasyExcel.write(outFilePath, DeptUserExcelEntity.class)
                                .sheet().doWrite(excelEntities);
                    }
                }
        );
    }

结果:

在这里插入图片描述

这里要注意的是因为调用该方法时回去修改对应的 class 对象,所以这里最好加锁

这种方式不仅可以处理 easyexcel 的注解动态表头问题,也可以处理传统的 poi 的注解动态表头,目前也是我用得比较多的一种方式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1581545.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java基于微信小程序的日语学习小程序,附源码

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

IMU状态预积分噪声模型

IMU状态预积分噪声模型 IMU状态预积分噪声模型旋转部分速度部分平移部分总结 IMU状态预积分噪声模型 根据之前的推导&#xff0c;得出了IMU状态预积分的测量模型&#xff0c;同时得到了噪声部分的定义公式&#xff0c;其中噪声部分罗列如下&#xff1a; 由于噪声项的定义比较复…

51单片机+TN901非接触式红外测温设计论文与源码PCB等资料

1、摘要 温度测量技术应用十分广泛&#xff0c;而且在现代设备故障检测领域中也是一项非常重要的技术。但在某些应用领域中&#xff0c;要求测量温度用的传感器不能与被测物体相接触&#xff0c;这就需要一种非接触的测温方式来满足上述测温需求。本论文正是应上述实际需求而设…

【2024】Rancher的安装与介绍

———————————————————————————— 记录一下rancher的学习与使用过程 本部分内容包括rancher的介绍、特点、与k8s关系和部署等内容 ———————————————————————————— Rancher是什么&#xff1f; 简单来说&#xff0c;Ranc…

RabbitMQ如何保证消息的幂等性???

在RabbitMQ中&#xff0c;保证消费者的幂等性主要依赖于业务设计和实现&#xff0c;而非RabbitMQ本身提供的一种直接功能。 在基于Spring Boot整合RabbitMQ的场景下&#xff0c;要保证消费者的幂等性&#xff0c;通常需要结合业务逻辑设计以及额外的技术手段来实现。以下是一个…

由近期 RAGFlow 的火爆看 RAG 的现状与未来

4 月 1 日&#xff0c;InfiniFlow &#xff08;英飞流&#xff09;的端到端 RAG 解决方案 RAGFlow 正式开源&#xff0c;首日即获得了 github 千星&#xff0c;目前已接近 3000 star。在这之前&#xff0c;InfiniFlow 还开源了专门用于 RAG 场景的 AI 原生数据库 Infinity&…

Emacs之实现复制当前已打开文件buffer(一百三十五)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

网络安全---非对称数据加密签名验证

一、课题描述 三位同学一组完成数据的非对称加密和数字签名验证传输。 三位同学分别扮演图中 Alice、Bob 和 CA 三个角色&#xff0c;Bob 和 Alice 从 CA 中获得数字证书、Bob 向 Alice 发送秘密发送一段加密并签名后的信息&#xff0c;Alice 获取 Bob 发送的加密信息&#x…

LeetCode 994—— 腐烂的橘子

阅读目录 1. 题目2. 解题思路3. 代码实现 1. 题目 2. 解题思路 1.记录下初始新鲜橘子的位置到 notRotting&#xff0c;我们按照行把二维数组拉成一维&#xff0c;所以&#xff0c;一个vector 就可以实现了&#xff1b;2.如果没有新鲜橘子&#xff0c;那么第 0 分钟所有橘子已经…

创建个人百度百科需要什么条件?

互联网时代&#xff0c;创建百度百科词条可以给个人带来更多的曝光和展现&#xff0c;相当于一个镀金的网络名片&#xff0c;人人都想上百度百科&#xff0c;但并不是人人都能创建上去的。 个人百度百科词条的创建需要满足一定的条件&#xff0c;今天伯乐网络传媒就来给大家聊聊…

Scrapy 爬取m3u8视频

Scrapy 爬取m3u8视频 【一】效果展示 爬取ts文件样式 合成的MP4文件 【二】分析m3u8文件路径 视频地址&#xff1a;[在线播放我独自升级 第03集 - 高清资源](https://www.physkan.com/ph/175552-8-3.html) 【1】找到m3u8文件 这里任务目标很明确 就是找m3u8文件 打开浏览器…

featup入坑笔记

一、新建环境 在conda中建立一个虚拟环境featup&#xff0c; conda create -n featup python3.9 二、开始配置&#xff1a; 我是先下载了FeatUp&#xff0c;之后 pip install -e . -i https://mirrors.aliyun.com/pypi/simple/ 但是&#xff0c;突然出错了&#xff0c;说无法…

Day:004(4) | Python爬虫:高效数据抓取的编程技术(数据解析)

XPath工具 浏览器-元素-CtrlF 浏览器-控制台- $x(表达式) Xpath helper (安装包需要科学上网) 问题 使用离线安装包 出现 程序包无效 解决方案 使用修改安装包的后缀名为 rar&#xff0c;解压文件到一个文件夹&#xff0c;再用 加载文件夹的方式安装即可 安装 python若使用…

【2024年5月备考新增】《软考案例分析答题技巧(3)质量、资源》

2.5 项目质量管理 质量管理过程 质量管理过程:规划质量管理-管理质量-控制质量。 管理质量意义: ① 通过执行有关产品特定方面的设计准则,设计出最优的成熟产品; ② 建立信心,相信通过质量保证工具和技术(如质量审计和故障分析)可以使未来输出在完工时满足特定的需求…

动态规划刷题(2)之杨辉三角(详细解释)

最近在自学动态规划,网上到处找资料学习: 在这里记录我的刷题历史: 题目都是在力扣里面刷的!! 这里,我放一个刷动态规划的链接在这里:动态规划知识点题库 - 力扣(LeetCode) 力扣 在这里附加动态规划相关知识点:动态规划(DP)-CSDN博客文章浏览阅读197次。动态规划…

postgresql uuid

示例数据库版本PG16&#xff0c;对于参照官方文档截图&#xff0c;可以在最上方切换到对应版本查看&#xff0c;相差不大。 方法一&#xff1a;自带函数 select gen_random_uuid(); 去掉四个斜杠&#xff0c;简化成32位 select replace(gen_random_uuid()::text, -, ); 官网介绍…

从数据中台到上层应用全景架构示例

一、前言 对于大型企业而言&#xff0c;数据已经成为基本的生产资料&#xff0c;但是有很多公司还是值关心上层应用&#xff0c;而忽略了数据的治理&#xff0c;从而并不能很好的发挥公司的数据资产效益。比如博主自己是做后端的&#xff0c;主要是做应用层&#xff0c;也就是…

(源码+部署+讲解)基于Spring Boot + Vue编程学习平台的设计与实现

前言 &#x1f497;博主介绍&#xff1a;✌专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f447;&#x1f3fb; 精彩专栏 推荐订阅&#x1f447;&#x1f3fb; 2024年Java精品实战案例《100套》 &#x1f345;文末获取源码联系&#x1f345; &#x1f31f;…

HDFS [MSST‘10] 论文阅读笔记

原论文:The Hadoop Distributed File System (MSST’10) HDFS关键技术要点概览 设计目标:HDFS旨在可靠地存储大型数据集,并以高带宽流式传输这些数据集到用户应用程序。它通过在大量服务器上分布存储和计算资源,使得资源可以随着需求的增长而扩展,同时保持经济高效。架构组…

Day:004(3) | Python爬虫:高效数据抓取的编程技术(数据解析)

BS4实战-人民网 人民网_网上的人民日报 (people.com.cn)http://www.people.com.cn/ import requests from fake_useragent import UserAgent from bs4 import BeautifulSoupurl http://www.people.com.cn/ headers {User-Agent:UserAgent().chrome} # 发送请求 resp request…