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 Methode besteht darin, Kommentare manuell in einer Designerdatei zu erstellen. Diese Kommentare werden dann mithilfe von Aspose.Cells importiert. Die zweite Methode besteht darin, Kommentare mithilfe der Aspose.Cells-API zur Laufzeit hinzuzufügen. In diesem Thema wird das Hinzufügen von Kommentaren zu Zellen mithilfe der Aspose.Cells-API erläutert. Auch die Formatierung von Kommentaren wird erklärt.
Einen Kommentar hinzufügen
Fügen Sie einen Kommentar zu einer Zelle hinzu, indem Sie die Hinzufügen-Methode der Comments-Sammlung (die in dem Objekt Worksheet verkapselt ist) aufrufen. Das neue Comment-Objekt kann von der Comments-Sammlung abgerufen werden, indem der Kommentarindex übergeben wird. Nach dem Zugriff auf das Comment-Objekt können Sie den Kommentarhinweis anpassen, indem Sie die Comment-Eigenschaft des Objekts Note verwenden.
// 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"); |
Kommentarformatierung
Es ist auch möglich, das Erscheinungsbild von Kommentaren zu formatieren, indem ihre Höhe, Breite und Schriftarteneinstellungen konfiguriert werden.
// 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"); |
Ein Bild zum Kommentar hinzufügen
Mit Microsoft Excel 2007 ist es auch möglich, ein Bild als Hintergrund für einen Zellenkommentar zu haben. In Excel 2007 wird dies durch die folgenden Schritte erreicht. (Es wird davon ausgegangen, dass bereits ein Zellenkommentar hinzugefügt wurde.)
- Klicken Sie mit der rechten Maustaste auf die Zelle, die den Kommentar enthält.
- Wählen Sie Kommentare einblenden/ausblenden und löschen Sie jeglichen Text aus dem Kommentar.
- Klicken Sie auf den Rand des Kommentars, um ihn auszuwählen.
- Wählen Sie Format und dann Kommentar aus.
- Auf der Registerkarte Farben und Linien die Farbe-Liste erweitern.
- Klicken Sie auf Fülleffekte.
- Klicken Sie auf der Registerkarte Bild auf Bild auswählen.
- Suchen Sie das Bild und wählen Sie es aus.
- Klicken Sie auf OK, bis alle Dialogfelder geschlossen sind.
Auch Aspose.Cells bietet diese Funktion. Im Folgenden finden Sie einen Beispielcode, der eine XLSX-Datei von Grund auf erstellt und einem Zelle “A1” einen Kommentar mit einem Bild als Hintergrund hinzufügt.
// 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"); |