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

相关推荐
SUPER52662 小时前
本地开发环境_spring-ai项目启动异常
java·人工智能·spring
moxiaoran57532 小时前
Spring AOP开发的使用场景
java·后端·spring
小王师傅667 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
醒过来摸鱼7 小时前
Java classloader
java·开发语言·python
专注于大数据技术栈7 小时前
java学习--StringBuilder
java·学习
loosenivy7 小时前
企业银行账户归属地查询接口如何用Java调用
java·企业银行账户归属地·企业账户查询接口·企业银行账户查询
IT 行者8 小时前
Spring Security 6.x 迁移到 7.0 的完整步骤
java·spring·oauth2
JIngJaneIL8 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
东东的脑洞8 小时前
【面试突击二】JAVA基础知识-volatile、synchronized与ReentrantLock深度对比
java·面试
川贝枇杷膏cbppg8 小时前
Redis 的 AOF
java·数据库·redis