当前位置: 首页 > 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 CommentWriteHandler(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;}}
}

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

相关文章:

  • HttpServletRequest・getContentLeng・getContentType区别
  • Matlab|【防骗帖】考虑时空相关性的风电功率预测误差建模与分析
  • 【Android面试八股文】说一说ListView卡顿的原因以及相对应的优化策略
  • Kotlin 中的内联函数
  • KALI LINUX 开启ssh免登录服务及固定ip及
  • 亮数据,一款新的低代码爬虫利器!
  • 配置OSPF认证(华为)
  • 关于ip地址的网页无法访问navigator的gpu、媒体、蓝牙等设备的解决方法
  • 深入理解外观模式(Facade Pattern)及其实际应用
  • 为什么永远不会有语言取代 C/C++?
  • Python 全栈体系【四阶】(六十一)
  • 工控必备C#
  • 【设计模式之基于特性的动态路由映射模式】
  • GB 16807-2009 防火膨胀密封件
  • 从零开始做题:老照片中的密码
  • 考研数学|张宇和武忠祥,强化能不能同时跟?
  • 【机器学习】——【线性回归模型】——详细【学习路线】
  • 【mysql】常用操作:维护用户/开启远程/忘记密码/常用命令
  • 引领AI新时代:深度学习与大模型的关键技术
  • STL——常用算法(二)
  • MyCAT 2 底层原理
  • 操作系统实训复习笔记(第7关:生产者消费者问题实践)
  • 通过物联网管理多台MQTT设备-基于全志T527开发板
  • Python学习前简介
  • 【Text2SQL 论文】MAGIC:为 Text2SQL 任务自动生成 self-correction guideline
  • 2024 年 8 款最佳建筑 3D 渲染软件
  • MAB规范(3):Chapter6 Glossary 术语表
  • 40python数据分析numpy基础之diag处理矩阵对角线元素
  • ffmpeg+nginx+video实现rtsp流转hls流,web页面播放
  • 1、Redis系列-Redis高性能原理详解