easyExcel实现表头批注

背景:

网上大部分都不能直接使用,为此总结一个方便入手且可用的工具,用自定义注解实现

依赖包:

复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.4</version>
</dependency>

实现过程:

1.自定义ExcelRemark注解

java 复制代码
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRemark {

    /**
     * 文本内容
     */
    String value( ) default "";

    /**
     * 批注行高, 一般不用设置
     * 这个参数可以设置不同字段 批注显示框的高度
     */
    int remarkRowHigh() default 0;

    /**
     * 批注列宽, 根据导出情况调整
     * 这个参数可以设置不同字段 批注显示框的宽度
     */
    int remarkColumnWide() default 0;
}

2.DTO

java 复制代码
public class regionDo {

    @ExcelProperty("省份")
    @ExcelRemark(value = "必填")
    private String province;

    @ExcelIgnore
    private String provinceCode;

    @ExcelProperty("地市")
    @ExcelRemark(value = "必填")
    private String city;
}

3.批注处理类

java 复制代码
public class CommentCellWriteHandler implements CellWriteHandler {
    
    private final Map<Integer, ExcelComment> notationMap;
 
    public CommentCellWriteHandler(Map<Integer, ExcelComment> notationMap) {
        this.notationMap = notationMap;
    }


    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

        //表头批注
        if (isHead){
            Sheet sheet = writeSheetHolder.getSheet();
            //画布
            Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
            if (!CollectionUtils.isEmpty(notationMap) && notationMap.containsKey(cell.getColumnIndex())){
                ExcelComment excelComment = notationMap.get(cell.getColumnIndex());
                if (Objects.nonNull(excelComment)){
                    Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), 0, (short) excelComment.getRemarkColumnWide(), 1));
                    comment.setString(new XSSFRichTextString(excelComment.getRemarkValue()));
                    cell.setCellComment(comment);
                }
            }
        }

    }

    /**
     * 获取批注Map
     *
     */
    public static Map<Integer, ExcelComment> getNotationMap(Class<?> clazz) {
        Map<Integer, ExcelComment> notationMap = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        //不使用下面方法,就必须指定@ExcelProperty的index
        int index = -1;
        for (Field field : fields) {
            ++index;
            if (!field.isAnnotationPresent(ExcelRemark.class)) {
                //不需要批注 并且 是无需导出字段则 索引回归
                if (field.isAnnotationPresent(ExcelIgnore.class)) {
                    --index;
                }
                continue;
            }
            //批注存放实体
            ExcelComment excelComment = new ExcelComment();
            //获取字段批注注解
            ExcelRemark ExcelRemark = field.getAnnotation(ExcelRemark.class);
            excelComment.setRemarkValue(ExcelRemark.value());
            excelComment.setRemarkColumnWide(ExcelRemark.remarkColumnWide());
            notationMap.put(index, excelComment);
        }
        return notationMap;
    }
 
}

5.注册器

java 复制代码
EasyExcel.write(response.getOutputStream(), RegionDo.class)
                    .registerWriteHandler(new CommentCellWriteHandler(CommentCellWriteHandler.getNotationMap(RegionDo.class)))
                    .sheet("sheet1")
                    ..doWrite(regionDoList)

引用:

https://blog.csdn.net/qq_43049310/article/details/130697234

https://blog.csdn.net/m0_61013974/article/details/134947917

相关推荐
奋进的芋圆3 小时前
Java 延时任务实现方案详解(适用于 Spring Boot 3)
java·spring boot·redis·rabbitmq
sxlishaobin3 小时前
设计模式之桥接模式
java·设计模式·桥接模式
model20053 小时前
alibaba linux3 系统盘网站迁移数据盘
java·服务器·前端
荒诞硬汉4 小时前
JavaBean相关补充
java·开发语言
提笔忘字的帝国4 小时前
【教程】macOS 如何完全卸载 Java 开发环境
java·开发语言·macos
2501_941882484 小时前
从灰度发布到流量切分的互联网工程语法控制与多语言实现实践思路随笔分享
java·开发语言
華勳全栈4 小时前
两天开发完成智能体平台
java·spring·go
alonewolf_995 小时前
Spring MVC重点功能底层源码深度解析
java·spring·mvc
沛沛老爹5 小时前
Java泛型擦除:原理、实践与应对策略
java·开发语言·人工智能·企业开发·发展趋势·技术原理
专注_每天进步一点点5 小时前
【java开发】写接口文档的札记
java·开发语言