在ms excel中,我们可以对指定的单元格添加批注,也可以对已有的批注进行修改以及删除操作。spire.xls组件也为开发者提供了相应的功能,本文将介绍如何使用spire.xls在excel文档中添加批注、修改批注及删除批注。
添加批注
c#
//新建excel文档
workbook workbook = new workbook();
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//设置批注文本
string comment = "comment by e-iceblue";
//设置批注字体
excelfont font = workbook.createfont();
font.fontname = "calibri";
font.color = color.firebrick;
font.isbold = true;
//添加批注
cellrange range = sheet.range["c6"];
range.comment.richtext.text = comment;
range.comment.width = 200;
range.comment.height = 50;
range.comment.richtext.setfont(11, 19, font);
//保存文档
workbook.savetofile("addcomment.xlsx", excelversion.version2013);
vb.net
'新建excel文档
dim workbook as new workbook()
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'设置批注文本
dim comment as string = "comment by e-iceblue"
'设置批注字体
dim font as excelfont = workbook.createfont()
font.fontname = "calibri"
font.color = color.firebrick
font.isbold = true
'添加批注
dim range as cellrange = sheet.range("c6")
range.comment.richtext.text = comment
range.comment.width = 200
range.comment.height = 50
range.comment.richtext.setfont(11, 19, font)
'保存文档
workbook.savetofile("addcomment.xlsx", excelversion.version2013)
效果图:
修改批注
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("addcomment.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//修改工作表中的第一个批注
sheet.comments[0].text = "new comment";
//保存文档
workbook.savetofile("modifycomment.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("addcomment.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'修改工作表中的第一个批注
sheet.comments(0).text = "new comment"
'保存文档
workbook.savetofile("modifycomment.xlsx", excelversion.version2013)
效果图:
删除批注
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("addcomment.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//删除工作表中的第一个批注
sheet.comments[0].remove();
//保存文档
workbook.savetofile("deletecomment.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("addcomment.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'删除工作表中的第一个批注
sheet.comments(0).remove()
'保存文档
workbook.savetofile("deletecomment.xlsx", excelversion.version2013)