在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)