Gérer les commentaires et notes
Introduction
Les commentaires sont utilisés pour ajouter des informations supplémentaires aux cellules. Aspose.Cells propose deux méthodes pour ajouter des commentaires aux cellules. La première consiste à créer des commentaires dans un fichier du concepteur manuellement. Ces commentaires sont ensuite importés en utilisant Aspose.Cells. La seconde consiste à ajouter des commentaires en utilisant l’API Aspose.Cells à l’exécution. Ce sujet traite de l’ajout de commentaires aux cellules en utilisant l’API Aspose.Cells. La mise en forme des commentaires sera également expliquée.
Ajouter un commentaire
Ajoutez un commentaire à une cellule en appelant la méthode Add de la collection Comments (encapsulée dans l’objet Worksheet). Le nouvel objet Comment peut être accédé à partir de la collection Comments en passant l’indice du commentaire. Après avoir accédé à l’objet Comment, personnalisez la note du commentaire en utilisant la propriété Note de l’objet Comment.
// 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"); |
Mise en forme des commentaires
Il est également possible de formater l’apparence des commentaires en configurant leur hauteur, leur largeur et leurs paramètres 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 en arrière-plan d’un commentaire de cellule. Dans Excel 2007, cela se fait en suivant les étapes suivantes. (Ils supposent que vous avez déjà ajouté un commentaire de cellule.)
- Faites un clic droit sur la cellule qui contient le commentaire.
- Sélectionnez Afficher/masquer les commentaires, et effacez tout texte du commentaire.
- Cliquez sur le bord du commentaire pour le sélectionner.
- Sélectionnez Format, puis Commentaire.
- Sur l’onglet Couleurs et traits, développez la liste Couleur.
- Cliquez sur Remplissage.
- Sur l’onglet Image, cliquez sur Sélectionner une image.
- Localisez et sélectionnez l’image.
- Cliquez sur OK jusqu’à ce que tous les dialogues se ferment.
Aspose.Cells propose également cette fonctionnalité. Ci-dessous se trouve 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); |