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

EasyExcel 导出批注信息

1. 批注信息

package com.xxx.demo;import lombok.Getter;/*** This class represents the comment information associated with a specific cell in an Excel sheet.* The columnIndex field specifies the column number of the cell, and the comment field stores the text of the comment.* The commentWidth and commentHeight fields specify the dimensions of the comment box.* If the width or height is not specified, default values are used.** @author xm.z*/
@Getter
public class CommentInfo {/*** Default width of the comment box (number of columns spanned).*/private static final int DEFAULT_COMMENT_WIDTH = 0;/*** Default height of the comment box (number of rows spanned).*/private static final int DEFAULT_COMMENT_HEIGHT = 0;/*** The index of the column where the comment should be added.*/private final int columnIndex;/*** The content of the comment.*/private final String comment;/*** The width of the comment box (number of columns spanned).*/private final int commentWidth;/*** The height of the comment box (number of rows spanned).*/private final int commentHeight;/*** Constructor to create a CommentInfo with default width and height.** @param columnIndex The index of the column where the comment should be added.* @param comment     The content of the comment.*/public CommentInfo(int columnIndex, String comment) {this(columnIndex, comment, DEFAULT_COMMENT_WIDTH, DEFAULT_COMMENT_HEIGHT);}/*** Constructor to create a CommentInfo with specified width and height.** @param columnIndex   The index of the column where the comment should be added.* @param comment       The content of the comment.* @param commentWidth  The width of the comment box (number of columns spanned).* @param commentHeight The height of the comment box (number of rows spanned).*/public CommentInfo(int columnIndex, String comment, int commentWidth, int commentHeight) {this.columnIndex = columnIndex;this.comment = comment;this.commentWidth = commentWidth;this.commentHeight = commentHeight;}}

2. 批注处理器

package com.xxx.demo;import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;import java.util.List;
import java.util.Map;/*** This class handles adding comments to specific cells in an Excel sheet written by EasyExcel.* It takes a map of row number to a list of CommentInfo objects, which define the comment content,* target cell index, width, and height.** @author xm.z*/
@Slf4j
public class CommentWriteHandler implements RowWriteHandler {private final Map<Integer, List<CommentInfo>> rowColumnCommentMap;/*** Constructor that takes a map of row number to a list of CommentInfo objects.** @param rowColumnCommentMap A map where the key is the row number (1-based) and the value is a list of CommentInfo objects for that row.*/public CommentWriteHandler(Map<Integer, List<CommentInfo>> rowColumnCommentMap) {this.rowColumnCommentMap = rowColumnCommentMap;}/*** This method is called after a row is written to the Excel sheet.* It iterates through the comments associated with the current row and adds them to the corresponding cells.** @param writeSheetHolder Holds information about the current sheet being written.* @param writeTableHolder Holds information about the current table being written.* @param row              The row that was just written.* @param relativeRowIndex The 0-based index of the row within the current sheet.* @param isHead           True if the row is the header row.*/@Overridepublic void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {Sheet sheet = row.getSheet();CreationHelper creationHelper = sheet.getWorkbook().getCreationHelper();Drawing<?> drawing = sheet.createDrawingPatriarch();// Get comments associated with the current row (1-based)List<CommentInfo> commentInfos = rowColumnCommentMap.get(row.getRowNum() + 1);if (commentInfos != null && !commentInfos.isEmpty()) {for (CommentInfo commentInfo : commentInfos) {addCommentToCell(row, creationHelper, drawing, commentInfo);}}}/*** This method adds a comment to a specific cell in the current row.** @param row            The row containing the cell to add the comment to.* @param creationHelper Used to create comment anchors and rich text strings.* @param drawing        The drawing patriarch used to create comments in the sheet.* @param commentInfo    An object containing information about the comment (content, target cell index, width, height).*/private void addCommentToCell(Row row, CreationHelper creationHelper, Drawing<?> drawing, CommentInfo commentInfo) {Cell cell = row.getCell(commentInfo.getColumnIndex());if (cell != null) {String value = cell.toString();ClientAnchor anchor = creationHelper.createClientAnchor();// Set anchor position and sizeanchor.setCol1(cell.getColumnIndex());anchor.setCol2(commentInfo.getCommentWidth());anchor.setRow1(row.getRowNum());anchor.setRow2(commentInfo.getCommentHeight());Comment comment = drawing.createCellComment(anchor);String commentContent = String.format(commentInfo.getComment(), value);comment.setString(creationHelper.createRichTextString(commentContent));cell.setCellComment(comment);}}
}

3. 测试导出

package com.xxx.demo;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;import java.util.*;/*** This class demonstrates how to export data to an Excel file with comments using EasyExcel.*/
public class ExcelExportDemo {public static void main(String[] args) {// Prepare demo dataList<DemoData> data = new ArrayList<>();data.add(new DemoData("张三", 25));data.add(new DemoData("李四", 30));// Define comment information for each row and columnMap<Integer, List<CommentInfo>> rowColumnCommentMap = new HashMap<>();// Add comments for row 2 (Name and Age columns)rowColumnCommentMap.put(2, Arrays.asList(new CommentInfo(0, "姓名:%s"),new CommentInfo(1, "年龄是:%s")));// Add comments for row 3 (Age column)rowColumnCommentMap.put(3, Collections.singletonList(new CommentInfo(1, "年龄:%s")));// Export data to Excel file with commentsEasyExcel.write("demo.xlsx", DemoData.class).inMemory(Boolean.TRUE).registerWriteHandler(new CustomCommentWriteHandler(rowColumnCommentMap)).sheet("Sheet1").doWrite(data);}@Dataprivate static class DemoData {@ExcelProperty("姓名")private String name;@ExcelProperty("年龄")private Integer age;public DemoData(String name, Integer age) {this.name = name;this.age = age;}}
}

相关文章:

  • 【Go】十四、图形验证码、短信验证码、注册接口与redis的简单使用
  • 单片机练习题3
  • 每日优秀影视分享❗❗
  • WPF文本绑定显示格式StringFormat设置-特殊格式时间日期和多数据绑定
  • 原生dom操作快速写入html渲染(insertAdjacentHTML)
  • Cadence:Conformal系列形式验证工具
  • 深入解析Netty的Reactor模型及其实现:详解与代码示例
  • Pikachu靶场--XSS
  • excel数据透视
  • Ubuntu常见命令解释
  • 修改主频睡眠模式停止模式待机模式
  • 第五章重采样方法
  • 牛顿迭代法(求解整数的近似平方根)
  • 网络爬虫中selenium和requests这两个工具有什么区别呢?
  • 力扣爆刷第153天之TOP100五连刷(接雨水、环形链表、最长上升子序列)
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • FastReport在线报表设计器工作原理
  • JS 面试题总结
  • js写一个简单的选项卡
  • VUE es6技巧写法(持续更新中~~~)
  • 编写高质量JavaScript代码之并发
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 大主子表关联的性能优化方法
  • 飞驰在Mesos的涡轮引擎上
  • 关于Java中分层中遇到的一些问题
  • 开源地图数据可视化库——mapnik
  • 入口文件开始,分析Vue源码实现
  • 试着探索高并发下的系统架构面貌
  • 我与Jetbrains的这些年
  • 一个SAP顾问在美国的这些年
  • 源码安装memcached和php memcache扩展
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 函数计算新功能-----支持C#函数
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #nginx配置案例
  • (1)svelte 教程:hello world
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (十五)、把自己的镜像推送到 DockerHub
  • .gitignore文件---让git自动忽略指定文件
  • .net core + vue 搭建前后端分离的框架
  • .NET 解决重复提交问题
  • .NET 快速重构概要1
  • .net/c# memcached 获取所有缓存键(keys)
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .NET6实现破解Modbus poll点表配置文件
  • .NET8使用VS2022打包Docker镜像
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • .net下的富文本编辑器FCKeditor的配置方法
  • .NET中的十进制浮点类型,徐汇区网站设计
  • // an array of int