Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > Cell单元格 >
如何:向单元格添加简单注释

本示例演示如何向单元格添加简单注释(旧注释)并设置注释文本的格式。

创建简单笔记

若要创建新注释并将其与单元格关联,请从 Worksheet.Comments 属性访问工作表的注释集合,然后调用 CommentCollection.Add 方法。

设置笔记内容的格式

若要将不同的字体应用于注释文本的特定区域,请修改 CommentRunCollection 属性返回的 CommentRunCollection 集合。此集合存储 CommentRun 对象,这些对象定义专门设置格式的注释文本区域。创建注释后,其文本由 CommentRunCollection 集合中包含的单个运行定义。

Vb.Net
Dim workbook As New DevExpress.Spreadsheet.Workbook()
Dim worksheet As DevExpress.Spreadsheet.Worksheet = workbook.Worksheets(0)

'获取系统用户名。
Dim author As String = workbook.CurrentAuthor

'在A1单元格中添加注释。
Dim cell As DevExpress.Spreadsheet.Cell = worksheet.Cells("A1")
Dim comment As DevExpress.Spreadsheet.Comment = worksheet.Comments.Add(cell, author, "This is important information for users.")

'在评论的开头添加作者姓名
Dim runs As DevExpress.Spreadsheet.CommentRunCollection = comment.Runs
runs.Insert(0, author & ": " & Constants.vbCrLf)
runs(0).Font.Bold = True

'设置注释文本的格式。
runs(1).Font.Color = Color.Red
runs(1).Font.Name = "Times New Roman"
runs(1).Font.Size = 14
runs(1).Font.Italic = True

'添加新的评论运行。 
runs.Add(Constants.vbLf & "Never delete this comment!")
runs(2).Font.Color = Color.MidnightBlue

C#
DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
DevExpress.Spreadsheet.Worksheet worksheet = workbook.Worksheets[0];

// 获取系统用户名。
string author = workbook.CurrentAuthor;

// 在A1单元格中添加注释。
DevExpress.Spreadsheet.Cell cell = worksheet.Cells["A1"];
DevExpress.Spreadsheet.Comment comment = worksheet.Comments.Add(cell, author, "This is important information for users.");

// 在评论的开头添加作者姓名
DevExpress.Spreadsheet.CommentRunCollection runs = comment.Runs;
runs.Insert(0, author + ": " + Constants.vbCrLf);
runs[0].Font.Bold = true;

// 设置注释文本的格式。
runs[1].Font.Color = Color.Red;
runs[1].Font.Name = "Times New Roman";
runs[1].Font.Size = 14;
runs[1].Font.Italic = true;

// 添加新的评论运行。
runs.Add(Constants.vbLf + "Never delete this comment!");
runs[2].Font.Color = Color.MidnightBlue;

 

删除笔记

使用以下方法删除简单注释: