Kommentare und Notizen verwalten
Einführung
Kommentare werden verwendet, um zusätzliche Informationen zu Zellen hinzuzufügen. Aspose.Cells bietet zwei Methoden zum Hinzufügen von Kommentaren zu Zellen. Die erste besteht darin, Kommentare manuell in einer Designerdatei zu erstellen. Diese Kommentare werden dann mit Aspose.Cells importiert. Die zweite besteht darin, Kommentare mit Aspose.Cells API zur Laufzeit hinzuzufügen. In diesem Thema wird das Hinzufügen von Kommentaren zu Zellen mit Aspose.Cells API behandelt. Das Formatieren von Kommentaren wird ebenfalls erläutert.
Hinzufügen eines Kommentars
Fügen Sie einer Zelle einen Kommentar hinzu, indem Sie die aufrufenBemerkungen SammlungAddieren Methode (eingekapselt in derArbeitsblatt Objekt). Das neueKommentar Auf das Objekt kann über die zugegriffen werdenBemerkungen Sammlung durch Übergabe des Kommentarindex. Nach dem Zugriff auf dieKommentar Objekt, passen Sie die Kommentarnotiz an, indem Sie das verwendenKommentar ObjektNotizEigentum.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a comment to "F5" cell | |
int commentIndex = worksheet.Comments.Add("F5"); | |
// Accessing the newly added comment | |
Comment comment = worksheet.Comments[commentIndex]; | |
// Setting the comment note | |
comment.Note = "Hello Aspose!"; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Kommentarformatierung
Es ist auch möglich, das Erscheinungsbild von Kommentaren zu formatieren, indem Sie ihre Höhe, Breite und Schriftarteinstellungen konfigurieren.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a comment to "F5" cell | |
int commentIndex = worksheet.Comments.Add("F5"); | |
// Accessing the newly added comment | |
Comment comment = worksheet.Comments[commentIndex]; | |
// Setting the comment note | |
comment.Note = "Hello Aspose!"; | |
// Setting the font size of a comment to 14 | |
comment.Font.Size = 14; | |
// Setting the font of a comment to bold | |
comment.Font.IsBold = true; | |
// Setting the height of the font to 10 | |
comment.HeightCM = 10; | |
// Setting the width of the font to 2 | |
comment.WidthCM = 2; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Fügen Sie ein Bild zum Kommentar hinzu
Mit Microsoft Excel 2007 ist es auch möglich, ein Bild als Hintergrund für einen Zellkommentar zu haben. In Excel 2007 wird dies durch Ausführen der folgenden Schritte erreicht. (Sie gehen davon aus, dass Sie bereits einen Zellenkommentar hinzugefügt haben.)
- Klicken Sie mit der rechten Maustaste auf die Zelle, die den Kommentar enthält.
- WählenKommentare ein-/ausblenden, und löschen Sie jeglichen Text aus dem Kommentar.
- Klicken Sie auf den Rand des Kommentars, um ihn auszuwählen.
- WählenFormat , dannKommentar.
- Auf derFarben und Linien Erweitern Sie die RegisterkarteFarbe aufführen.
- KlickenFülleffekte.
- Auf derBild Registerkarte, klicken Sie aufWählen Sie Bild.
- Suchen Sie das Bild und wählen Sie es aus.
- KlickenOK bis alle Dialoge geschlossen sind.
Aspose.Cells bietet diese Funktion ebenfalls. Unten ist ein Codebeispiel, das eine XLSX-Datei von Grund auf neu erstellt, indem ein Kommentar zur Zelle “A1” mit einem als Hintergrund festgelegten Bild hinzugefügt wird.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a Workbook | |
Workbook workbook = new Workbook(); | |
// Get a reference of comments collection with the first sheet | |
CommentCollection comments = workbook.Worksheets[0].Comments; | |
// Add a comment to cell A1 | |
int commentIndex = comments.Add(0, 0); | |
Comment comment = comments[commentIndex]; | |
comment.Note = "First note."; | |
comment.Font.Name = "Times New Roman"; | |
// Load an image into stream | |
Bitmap bmp = new Bitmap(dataDir + "logo.jpg"); | |
MemoryStream ms = new MemoryStream(); | |
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); | |
// Set image data to the shape associated with the comment | |
comment.CommentShape.Fill.ImageData = ms.ToArray(); | |
// Save the workbook | |
workbook.Save(dataDir + "book1.out.xlsx", Aspose.Cells.SaveFormat.Xlsx); |