在工作表中插入和移除单元格批注

在单元格上添加和移除批注

要向单元格添加注释:

  1. 打开一个现有的Excel文件。
  2. 向单元格添加评论。
  3. 保存文件。

要删除注释,该过程类似,唯一不同之处在于注释被移除。

下面的代码示例首先说明如何使用VSTO或Aspose.Cells for .NET来添加注释,然后说明如何删除注释

插入注释

这些代码片段展示了如何首先使用VSTO(C#,VB),然后使用Aspose.Cells for .NET(C#,VB)向单元格添加注释。

使用 VSTO 插入批注

C#

 .......

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Excel = Microsoft.Office.Interop.Excel;

using Office = Microsoft.Office.Core;

using System.Reflection;

.......

//Instantiate the Application object.

Excel.Application excelApp = new Excel.ApplicationClass();

//Specify the template excel file path.

string myPath=@"d:\test\Book1.xls";

//Open the excel file.

excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value);

//Get the A1 cell.

Excel.Range rng1=excelApp.get_Range("A1", Missing.Value);

//Add the comment with text.

rng1.AddComment("This is my comment");

//Save the file.

excelApp.ActiveWorkbook.Save();

//Quit the Application.

excelApp.Quit();

使用Aspose.Cells for .NET插入注释

C#

 .......

using Aspose.Cells;

.......

//Specify the template excel file path.

string myPath=@"d:\test\Book1.xls";

//Instantiate a new Workbook.

//Open the excel file.

Workbook workbook = new Workbook(myPath);

//Add a Comment to A1 cell.

int commentIndex=workbook.Worksheets[0].Comments.Add("A1");

//Accessing the newly added comment

Comment comment=workbook.Worksheets[0].Comments[commentIndex];

//Setting the comment note

comment.Note="This is my comment";

//Save As the excel file.

workbook.Save(@"d:\test\Book1.xls");

移除评论

要从单元格中移除批注,使用以下代码行来从 VSTO(C#、VB)和Aspose.Cells for .NET(C#、VB)移除批注。

使用 VSTO 移除批注

C#

 //Remove the comment.

rng1.Comment.Delete();    

使用Aspose.Cells for .NET删除注释

C#

 //Remove the comment.

workbook.Worksheets[0].Comments.RemoveAt("A1");