在 VSTO 和 Aspose.Cells 的工作表中插入和删除 Cell 评论

向单元格添加注释:

  1. 打开现有的 Excel 文件。
  2. 向单元格添加注释。
  3. 保存文件。

要删除评论,过程类似,只是删除评论。

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

插入评论

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

VSTO

 //Instantiate the Application object.

 Excel.Application excelApp = Application;

//Specify the template excel file path.

  string myPath = "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

 //Specify the template excel file path.

string myPath = "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("Book1.xls");

删除评论

若要从单元格中删除注释,请对 VSTO (C#) 和 Aspose.Cells for .NET (C#) 使用以下代码行。

VSTO

 //Remove the comment.

  rng1.Comment.Delete();

Aspose.Cells

 //removing comments

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

下载示例代码