Gérer les commentaires et les notes
Introduction
Les commentaires sont utilisés pour ajouter des informations supplémentaires aux cellules. Aspose.Cells fournit deux méthodes pour ajouter des commentaires aux cellules. La première consiste à créer manuellement des commentaires dans un fichier de concepteur. Ces commentaires sont ensuite importés à l’aide de Aspose.Cells. La seconde consiste à ajouter des commentaires à l’aide de Aspose.Cells API au moment de l’exécution. Cette rubrique traite de l’ajout de commentaires aux cellules à l’aide du Aspose.Cells API. La mise en forme des commentaires sera également expliquée.
Ajout d’un commentaire
Ajouter un commentaire à une cellule en appelant lecommentaires de la collectionAjouter méthode (encapsulée dans laFeuille de travail objet). Le nouveauCommentaire l’objet est accessible depuis lecommentaires collection en passant l’index de commentaire. Après avoir accédé auCommentaire objet, personnalisez la note de commentaire à l’aide deCommentaire objetsNoterla propriété.
// 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"); |
Formatage des commentaires
Il est également possible de formater l’apparence des commentaires en configurant leurs paramètres de hauteur, de largeur et de police.
// 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"); |
Ajouter une image au commentaire
Avec Microsoft Excel 2007, il est également possible d’avoir une image comme arrière-plan d’un commentaire de cellule. Dans Excel 2007, cela se fait en procédant comme suit. (Ils supposent que vous avez déjà ajouté un commentaire de cellule.)
- Cliquez avec le bouton droit sur la cellule qui contient le commentaire.
- SélectionnerAfficher/Masquer les commentaires, et effacez tout texte du commentaire.
- Cliquez sur la bordure du commentaire pour le sélectionner.
- SélectionnerFormat , ensuiteCommentaire.
- Sur leCouleurs et lignes onglet, développez l’ongletCouleur liste.
- Cliquez surEffets de remplissage.
- Sur lePhoto onglet, cliquezSélectionnez l’image.
- Localisez et sélectionnez l’image.
- Cliquez surD’ACCORD jusqu’à ce que toutes les boîtes de dialogue soient fermées.
Aspose.Cells fournit également cette fonctionnalité. Vous trouverez ci-dessous un exemple de code qui crée un fichier XLSX à partir de zéro, en ajoutant un commentaire à la cellule “A1” avec une image définie comme arrière-plan.
// 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); |