Hantera kommentarer och anteckningar

Introduktion

Kommentarer används för att lägga till ytterligare information till celler. Aspose.Cells tillhandahåller två metoder för att lägga till kommentarer till celler. Det första är att skapa kommentarer i en designerfil manuellt. Dessa kommentarer importeras sedan med Aspose.Cells. Den andra är att lägga till kommentarer med Aspose.Cells API under körning. Det här ämnet diskuterar att lägga till kommentarer till celler med hjälp av Aspose.Cells API. Formatering av kommentarer kommer också att förklaras.

Lägger till en kommentar

Lägg till en kommentar till en cell genom att anropaKommentarer samlingensLägg till metod (inkapslad iArbetsblad objekt). Den nyaKommentar objekt kan nås frånKommentarer insamling genom att passera kommentarsindex. Efter att ha kommit åtKommentar objekt, anpassa kommentaren genom att användaKommentar föremålNoterafast egendom.

// 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");

Kommentarsformatering

Det är också möjligt att formatera kommentarers utseende genom att konfigurera deras höjd, bredd och teckensnitt.

// 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");

Lägg till en bild för att kommentera

Med Microsoft Excel 2007 är det också möjligt att ha en bild som bakgrund till en cellkommentar. I Excel 2007 uppnås detta genom att utföra följande steg. (De antar att du redan har lagt till en cellkommentar.)

  1. Högerklicka på cellen som innehåller kommentaren.
  2. VäljVisa/dölj kommentarer, och ta bort all text från kommentaren.
  3. Klicka på kommentarens kant för att välja den.
  4. VäljFormatera , dåKommentar.
  5. Färger och linjer flik, expanderaFärg lista.
  6. KlickFyllningseffekter.
  7. Bild fliken, klickaVälj Bild.
  8. Leta upp och välj bilden.
  9. KlickOK tills alla dialogrutor har stängts.

Aspose.Cells tillhandahåller också denna funktion. Nedan finns ett kodexempel som skapar en XLSX-fil från grunden och lägger till en kommentar till cell “A1” med en bild som bakgrund.

// 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");

Förhandsämnen