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 Ajouter de la collection Comments (encapsulée dans l’objet Worksheet). Le nouvel objet Comment peut être accédé depuis la collection Comments en passant l’index 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-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a comment to "F5" cell
int commentIndex = worksheet.getComments().add("F5");
// Accessing the newly added comment
com.aspose.cells.Comment comment = worksheet.getComments().get(commentIndex);
// Setting the comment note
comment.setNote( "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-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a comment to "F5" cell
int commentIndex = worksheet.getComments().add("F5");
// Accessing the newly added comment
com.aspose.cells.Comment comment = worksheet.getComments().get(commentIndex);
// Setting the comment note
comment.setNote( "Hello Aspose!");
// Setting the font size of a comment to 14
comment.getFont().setSize(14);
// Setting the font of a comment to bold
comment.getFont().setBold(true);
// Setting the height of the font to 10
comment.setHeightCM(10);
// Setting the width of the font to 2
comment.setWidthCM(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.)

  1. Faites un clic droit sur la cellule qui contient le commentaire.
  2. Sélectionnez Afficher/masquer les commentaires, et effacez tout texte du commentaire.
  3. Cliquez sur le bord du commentaire pour le sélectionner.
  4. Sélectionnez Format, puis Commentaire.
  5. Sur l’onglet Couleurs et traits, développez la liste Couleur.
  6. Cliquez sur Remplissage.
  7. Sur l’onglet Image, cliquez sur Sélectionner une image.
  8. Localisez et sélectionnez l’image.
  9. 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-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get a reference of comments collection with the first sheet
com.aspose.cells.CommentCollection comments = workbook.getWorksheets().get(0).getComments();
// Add a comment to cell A1
int commentIndex = comments.add(0, 0);
com.aspose.cells.Comment comment = comments.get(commentIndex);
comment.setNote("First note.");
comment.getFont().setName("Times New Roman");
// Load an image
String filename = dataDir + "image.jpg";
byte[] result = null;
java.nio.channels.FileChannel fc = null;
try {
fc = new java.io.RandomAccessFile(filename, "r").getChannel();
java.nio.MappedByteBuffer byteBuffer = fc.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
byteBuffer.get(result, 0, byteBuffer.remaining());
}
} catch (IOException e) {
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
throw e;
}
}
// Set image data to the shape associated with the comment
comment.getCommentShape().getFill().setImageData(result);
// Saving the Excel file
workbook.save(dataDir + "book1.out.xlsx");

Sujets avancés