这篇文章主要为大家详细介绍了C#如何在Excel表格中插入、编辑和删除批注,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
正文
C#在Excel表格中插入、编辑和删除批注
概述
为文档添加必要的批注可以给文档使用者提供重要的提示信息,下面的示例中,将介绍通过c#编程语言来给excel表格中的指定单元格内容添加批注,此外,对于已有的批注,如果需要修改,我们也可以进行编辑或者删除批注。示例内容将包含以下主要内容:
1.插入批注
1.1 插入文本
1.2 插入图片
2.编辑批注
2.1 修改批注内容
2.1 设置批注可见性
3.删除批注
工具
提示:在进行代码操作之前,需下载安装spire.xls,并添加引用dll文件,添加如下using指令
using system;
using spire.xls;
using system.drawing;
代码示例(供参考)
1.插入excel批注
【c#】
步骤1:实例化一个workbook类实例并加载excel文档
|
1
2
|
workbook workbook = new workbook();workbook.loadfromfile("test.xlsx"); |
步骤2:获取第一个工作表
|
1
|
worksheet sheet = workbook.worksheets[0]; |
步骤3:插入文本批注
|
1
2
3
4
5
6
7
8
9
10
|
string comment = "注意:\n 责任人兼设备维护人";//设置批注文本excelfont font = workbook.createfont();//设置批注字体格式font.fontname = "calibri";font.color = color.black;font.isbold = true;cellrange range = sheet.range["i3"];//添加批注到指定单元格range.comment.richtext.text = comment;range.comment.width = 200;range.comment.height = 50;range.comment.richtext.setfont(10, 10, font); |
步骤4:插入图片批注
|
1
2
3
4
5
|
//加载图片,将图片插入到指定单元格的批注image image = image.fromfile("logo.png");sheet.range["b2"].comment.fill.custompicture(image, "logo.png");sheet.range["b2"].comment.height = image.height;sheet.range["b2"].comment.width = image.width; |
步骤5:保存文档
|
1
2
|
workbook.savetofile("addcomment.xlsx", excelversion.version2013);system.diagnostics.process.start("addcomment.xlsx"); |
批注插入效果(如下图):

全部代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
using system;using spire.xls;using system.drawing;namespace modifycomment_xls{ class program { static void main(string[] args) { //实例化一个workbook类实例并加载excel文档 workbook workbook = new workbook(); workbook.loadfromfile("test.xlsx"); //获取第一个工作表 worksheet sheet = workbook.worksheets[0]; //设置批注文本 string comment = "注意:\n 责任人兼设备维护人"; //设置批注字体 excelfont font = workbook.createfont(); font.fontname = "calibri"; font.color = color.black; font.isbold = true; //添加批注到指定单元格 cellrange range = sheet.range["i3"]; range.comment.richtext.text = comment; range.comment.width = 200; range.comment.height = 50; range.comment.richtext.setfont(10, 10, font); //加载图片,将图片插入到指定单元格的批注 image image = image.fromfile("logo.png"); sheet.range["b2"].comment.fill.custompicture(image, "logo.png"); sheet.range["b2"].comment.height = image.height; sheet.range["b2"].comment.width = image.width; //保存并打开文档 workbook.savetofile("addcomment.xlsx", excelversion.version2013); system.diagnostics.process.start("addcomment.xlsx"); } }} |
2. 修改、隐藏excel批注
【c#】
步骤1:创建一个workbook类对象,并加载excel文档
|
1
2
|
workbook workbook = new workbook();workbook.loadfromfile("addcomment.xlsx"); |
步骤2:获取第一个工作表
|
1
|
worksheet sheet = workbook.worksheets[0]; |
步骤3:修改工作表中的第一个批注
|
1
2
|
excelcomment comment0 = workbook.worksheets[0].comments[0];sheet.comments[0].text = "this is a new comment"; |
步骤4:设置批注可见性(隐藏、显示)
|
1
2
3
4
|
//设置指定批注不可见(隐藏)sheet.comments[0].isvisible = true;//设置指定批注可见(显示)sheet.comments[1].isvisible = false; |
步骤5:保存文档
|
1
2
|
workbook.savetofile("modifycomment.xlsx", excelversion.version2013);system.diagnostics.process.start("modifycomment.xlsx"); |
效果图:

全部代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using system;using spire.xls;using system.drawing;namespace modifycomment_xls{ class program { static void main(string[] args) { //创建一个workbook类对象,并加载excel文档 workbook workbook = new workbook(); workbook.loadfromfile("addcomment.xlsx"); //获取第一个工作表 worksheet sheet = workbook.worksheets[0]; //修改工作表中的第一个批注 excelcomment comment0 = workbook.worksheets[0].comments[0]; sheet.comments[0].text = "this is a new comment"; //设置指定批注不可见(隐藏) sheet.comments[0].isvisible = true; //设置指定批注可见(显示) sheet.comments[1].isvisible = false; //保存并打开文档 workbook.savetofile("modifycomment.xlsx", excelversion.version2013); system.diagnostics.process.start("modifycomment.xlsx"); } }} |
3.删除excel批注
【c#】
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//实例化wordbook类实例并加载excel文档workbook workbook = new workbook();workbook.loadfromfile("comments.xlsx");//获取第一个工作表worksheet sheet = workbook.worksheets[0];//删除工作表中的第2个批注sheet.comments[1].remove();//保存并打开文档workbook.savetofile("removecomment.xlsx", excelversion.version2013);system.diagnostics.process.start("removecomment.xlsx"); |
以上全部为本篇文章的全部内容,希望对大家的学习有所帮助,也希望大家多多支持米米素材网。
原文链接:https://www.cnblogs.com/Yesi/p/9187383.html

发表评论