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

相关文章:

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 te…...

HttpServletRequest・getContentLeng・getContentType区别

getContentLength()&#xff1a; 获取客户端发送到服务器的HTTP请求主体内容的字节数&#xff08;长度&#xff09; 如果请求没有正文内容&#xff08;如GET&#xff09;&#xff0c;或者请求头中没有包含Content-Length字段&#xff0c;则该方法返回 -1 getContentType()&am…...

Matlab|【防骗帖】考虑时空相关性的风电功率预测误差建模与分析

目录 1 主要内容 2 部分程序 3 下载链接 1 主要内容 这个程序《考虑时空相关性的风电功率预测误差建模与分析》画的图片非常漂亮&#xff0c;和原文献基本一致&#xff0c;但是实际上内容并未实现出来&#xff0c;主要就是利用现有的风电预测的数据和结果做了相关的图&#…...

【Android面试八股文】说一说ListView卡顿的原因以及相对应的优化策略

文章目录 一、ListView卡顿的原因1.1 Item没有复用1.2 布局的层级过深1.3 数据绑定逻辑过多1.4 滑动时不必要的图片刷新1.5 频繁的notifyDataSetChanged二、优化策略2.1 使用 ViewHolder 进行视图复用2.2 优化布局结构2.3 优化数据绑定逻辑过多2.4 图片加载优化2.5 避免频繁调用…...

Kotlin 中的内联函数

1 inline 内联函数&#xff1a;消除 Lambda 带来的运行时开销。 举例来说&#xff1a; fun main() {val num1 100val num2 80val result num1AndNum2(num1, num2) { n1, n2 ->n1 n2} }fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int …...

KALI LINUX 开启ssh免登录服务及固定ip及

SSH以进行远程登录 在Kali Linux中启用SSH以进行远程登录,请按照以下步骤操作: 安装SSH服务:sudo apt update sudo apt install openssh-server 已安装可忽略 sudo systemctl start ssh 启动SSH服务 sudo systemctl enable ssh 确保SSH服务设置为开机启动: (可选)如…...

亮数据,一款新的低代码爬虫利器!

在当今数据驱动型时代&#xff0c;数据采集和分析能力算是个人和企业的核心竞争力。然而&#xff0c;手动采集数据耗时费力且效率低下&#xff0c;而且容易被网站封禁。 我之前使用过一个爬虫工具&#xff0c;亮数据&#xff08;Bright Data&#xff09; &#xff0c;是一款低…...

配置OSPF认证(华为)

#交换设备 配置OSPF认证-基于华为路由器 OSPF&#xff08;开放最短路径优先&#xff09;是一种内部网关协议&#xff08;IGP&#xff09;&#xff0c;用于在单一自治系统&#xff08;AS&#xff09;内决策路由。OSPF认证功能是路由器中的一项安全措施&#xff0c;它的主要用途…...

关于ip地址的网页无法访问navigator的gpu、媒体、蓝牙等设备的解决方法

在使用threejs的WebGPURenderer渲染器时&#xff0c;发现localhost以及127.0.0.1才能访问到navigator.gpu&#xff0c;直接使用ip会变成undefined,原因是为了用户的隐私安全&#xff0c;只能在安全的上下文中使用&#xff0c;非安全的上下文就会是undefined&#xff0c;安全上下…...

深入理解外观模式(Facade Pattern)及其实际应用

引言 在软件开发中&#xff0c;复杂的系统往往由多个子系统组成&#xff0c;这些子系统之间的交互可能非常复杂。外观模式&#xff08;Facade Pattern&#xff09;通过为这些子系统提供一个统一的接口&#xff0c;简化了它们的交互。本篇文章将详细介绍外观模式的概念、应用场…...

为什么永远不会有语言取代 C/C++?

每个 CPU 都带有一种称为 ISA&#xff08;指令集架构&#xff09;汇编的电路语言。ISA 程序集是一种硬件语言&#xff0c;由基本数据操作、数学计算和结构化编程&#xff08;即 jmp&#xff09;的操作组成。但是&#xff0c;为每个计算需求编写汇编代码无疑是耗时的&#xff0c…...

Python 全栈体系【四阶】(六十一)

第五章 深度学习 十三、自然语言处理&#xff08;NLP&#xff09; 5. NLP应用 5.2 文本情感分析 目标&#xff1a;利用训练数据集&#xff0c;对模型训练&#xff0c;从而实现对中文评论语句情感分析。情绪分为正面、负面两种 数据集&#xff1a;中文关于酒店的评论&#…...

工控必备C#

微软的C# 语言&#xff1f; QT 熟了以后,Qt 更方便些 方法Signal Slot 感觉上一样 现在更推荐PyQt 来构建,底层还是Qt C 的那些库,Qt 的开源协议有点狗...

【设计模式之基于特性的动态路由映射模式】

在ASP.NET Core中&#xff0c;路由是核心功能之一&#xff0c;用于将HTTP请求映射到相应的控制器操作。虽然“路由驱动设计模式”是一个我刚杜撰出来的设计模式名称&#xff0c;但我们可以基于ASP.NET Core的路由特性&#xff0c;构建一种以路由为中心的设计模式。 以下是一个…...

GB 16807-2009 防火膨胀密封件

防火膨胀密封件是指在火灾时遇火或高温作用能够膨胀&#xff0c;且能辅助建筑构配件使之具有隔火、隔烟、隔热等防火密封性能的产品。 GB 16807-2009 防火膨胀密封件测试项目 测试要求 测试标准 外观 GB 16807 尺寸允许偏差 GB 16807 膨胀性能 GB 16807 产烟毒性 GB …...

从零开始做题:老照片中的密码

老照片中的密码 1.题目 1.1 给出图片如下 1.2 给出如下提示 这张老照片中的人使用的是莫尔斯电报机&#xff0c;莫尔斯电报机分为莫尔斯人工电报机和莫尔斯自动电报机&#xff08;简称莫尔斯快机&#xff09;。莫尔斯人工电报机是一种最简单的电报机&#xff0c;由三个部分组…...

考研数学|张宇和武忠祥,强化能不能同时跟?

可以说你跟武老师学明白了&#xff0c;120完全没问题&#xff01;如果追求更高&#xff0c;宇哥的怀抱也想你敞开&#xff01; 学长我21年一战数学83&#xff0c;总分没过线&#xff0c;22年二战143&#xff0c;逆袭上岸211&#xff01;市面上的老师我基本都听过&#xff0c;最…...

【机器学习】——【线性回归模型】——详细【学习路线】

目录 1. 引言 2. 线性回归理论基础 2.1 线性模型概述 2.2 最小二乘法 3. 数学基础 3.1 矩阵运算 3.2 微积分 3.3 统计学 4. 实现与应用 4.1 使用Scikit-learn实现线性回归 4.2 模型评估 5. 深入理解 5.1 多元线性回归 5.2 特征选择 5.3 理解模型内部 6. 实战与项…...

【mysql】常用操作:维护用户/开启远程/忘记密码/常用命令

一、维护用户 1.1 创建用户 -- 语法 > CREATE USER [username][host] IDENTIFIED BY [password];-- 例子&#xff1a; -- 添加用户user007&#xff0c;密码123456&#xff0c;并且只能在本地可以登录 > CREATE USER user007localhost IDENTIFIED BY 123456; -- 添加用户…...

引领AI新时代:深度学习与大模型的关键技术

文章目录 &#x1f4d1;前言一、内容概述二、作者简介三、书籍特色四、学习平台与资源 &#x1f4d1;前言 在数字化浪潮席卷全球的今天&#xff0c;人工智能&#xff08;AI&#xff09;和深度学习技术已经渗透到我们生活的方方面面。从智能手机中的智能语音助手&#xff0c;到…...

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

解决Ubuntu22.04 VMware失败的问题 ubuntu入门之二十八

现象1 打开VMware失败 Ubuntu升级之后打开VMware上报需要安装vmmon和vmnet&#xff0c;点击确认后如下提示 最终上报fail 解决方法 内核升级导致&#xff0c;需要在新内核下重新下载编译安装 查看版本 $ vmware -v VMware Workstation 17.5.1 build-23298084$ lsb_release…...

关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案

问题描述&#xff1a;iview使用table 中type: "index",分页之后 &#xff0c;索引还是从1开始&#xff0c;试过绑定后台返回数据的id, 这种方法可行&#xff0c;就是后台返回数据的每个页面id都不完全是按照从1开始的升序&#xff0c;因此百度了下&#xff0c;找到了…...

使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台

🎯 使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台 📌 项目背景 随着大语言模型(LLM)的广泛应用,开发者常面临多个挑战: 各大模型(OpenAI、Claude、Gemini、Ollama)接口风格不统一;缺乏一个统一平台进行模型调用与测试;本地模型 Ollama 的集成与前…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

AGain DB和倍数增益的关系

我在设置一款索尼CMOS芯片时&#xff0c;Again增益0db变化为6DB&#xff0c;画面的变化只有2倍DN的增益&#xff0c;比如10变为20。 这与dB和线性增益的关系以及传感器处理流程有关。以下是具体原因分析&#xff1a; 1. dB与线性增益的换算关系 6dB对应的理论线性增益应为&…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

【C++特殊工具与技术】优化内存分配(一):C++中的内存分配

目录 一、C 内存的基本概念​ 1.1 内存的物理与逻辑结构​ 1.2 C 程序的内存区域划分​ 二、栈内存分配​ 2.1 栈内存的特点​ 2.2 栈内存分配示例​ 三、堆内存分配​ 3.1 new和delete操作符​ 4.2 内存泄漏与悬空指针问题​ 4.3 new和delete的重载​ 四、智能指针…...

PHP 8.5 即将发布:管道操作符、强力调试

前不久&#xff0c;PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5&#xff01;作为 PHP 语言的又一次重要迭代&#xff0c;PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是&#xff0c;借助强大的本地开发环境 ServBay&am…...