Einfügen und Entfernen von Cell-Kommentaren in einem Arbeitsblatt

Hinzufügen und Entfernen von Kommentaren zu Cells

So fügen Sie Kommentare zu Zellen hinzu:

  1. Öffnen Sie eine vorhandene Excel-Datei.
  2. Fügen Sie einer Zelle einen Kommentar hinzu.
  3. Speicher die Datei.

Um die Kommentare zu entfernen, ist der Vorgang ähnlich, mit der Ausnahme, dass der Kommentar entfernt wird.

Die folgenden Codebeispiele veranschaulichen zunächst, wie das gehtEinen Kommentar hinzufügen und dann wieeinen Kommentar entfernen entweder mit VSTO oder Aspose.Cells for .NET.

Kommentare einfügen

Diese Codeschnipsel zeigen, wie man zuerst mit einen Kommentar zu einer Zelle hinzufügtVSTO (C#, VB) und dann mitAspose.Cells for .NET (C#, VB).

Einfügen eines Kommentars mit 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();

Einfügen eines Kommentars mit 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");

Kommentare entfernen

Um einen Kommentar aus einer Zelle zu entfernen, verwenden Sie die folgenden Codezeilen fürVSTO (C#, VB) undAspose.Cells for .NET (C#, VB).

Entfernen eines Kommentars mit VSTO

C#

 //Remove the comment.

rng1.Comment.Delete();    

Entfernen eines Kommentars mit Aspose.Cells for .NET

C#

 //Remove the comment.

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