当前位置: 首页 > news >正文

easyExcel实现表头批注

背景:

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

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.4</version>
</dependency>

实现过程:

1.自定义ExcelRemark注解

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRemark {/*** 文本内容*/String value( ) default "";/*** 批注行高, 一般不用设置* 这个参数可以设置不同字段 批注显示框的高度*/int remarkRowHigh() default 0;/*** 批注列宽, 根据导出情况调整* 这个参数可以设置不同字段 批注显示框的宽度*/int remarkColumnWide() default 0;
}

2.DTO

public class regionDo {@ExcelProperty("省份")@ExcelRemark(value = "必填")private String province;@ExcelIgnoreprivate String provinceCode;@ExcelProperty("地市")@ExcelRemark(value = "必填")private String city;
}

3.批注处理类

public class CommentCellWriteHandler implements CellWriteHandler {private final Map<Integer, ExcelComment> notationMap;public CommentCellWriteHandler(Map<Integer, ExcelComment> notationMap) {this.notationMap = notationMap;}@Overridepublic 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的indexint 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.注册器

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

http://www.lryc.cn/news/500999.html

相关文章:

  • Pytest测试用例使用小结
  • LeetCode题练习与总结:132 模式--456
  • IdentityServer4框架、ASP.NET core Identity
  • 【分子材料发现】——GAP:催化过程中吸附构型的多模态语言和图学习(数据集处理详解)(二)
  • SpringBoot开发过程中经常遇到问题解决方案分享
  • AR眼镜_消费级工业AR智能眼镜主板硬件解决方案
  • Springboot 核心注解
  • Nacos集群搭建【Oracle作外部数据源】
  • 云轴科技ZStack出席中国电信国际EMCP平台香港发布会,持续推动海外合作
  • 爬虫自动化之drissionpage+SwitchyOmega实现随时切换代理ip
  • docker安装kettle(PDI)并实现web访问
  • [软件工程]十.可靠性工程(reliable engineering)
  • 【Makefile】编译日志之输出重定向符号 >
  • linux之less
  • 算法-字符串-165.比较版本号
  • List与Set、数组与ArrayList、ArrayList与LinkedList的区别
  • 如何在 Odoo18 视图中添加关联数据看板按钮 | 免费开源ERP实施诀窍
  • Linux下mysql环境的搭建
  • 视觉语言模型 Qwen2-VL
  • 浅谈新能源汽车感应钥匙一键启动的步骤和特点
  • 鸿蒙ArkTS语言基础语法详解
  • H5游戏出海如何获得更多增长机会?
  • Cmake+基础命令
  • python数据分析之爬虫基础:requests详解
  • PHP期末复习(通过30道填空题梳理知识点)
  • PostgreSQL 安装部署系列:使用YUM 方式在Centos 7.9 安装指定 PostgreSQL -15版本数据库
  • 知识图谱8:深度学习各种小模型
  • 为什么 JavaScript 中的 `new` 运算符报错?
  • Tomcat,javaweb, servlet , springBoot
  • 使用Kimi开发自己的问答应用