如何在Excel中更改评论的背景

如何在Excel中更改评论的颜色

当你不需要默认的评论背景颜色时,你可能希望用你感兴趣的颜色来替换它。如何在Excel中更改评论框的背景颜色?

以下代码将指导你如何使用Aspose.Cells为你自己选择的评论添加喜欢的背景颜色。

在这里,我们已为您准备了一个样本文件。这个文件用于在下面的代码中初始化Workbook对象。

// 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 path = "";
//Initialize a new workbook.
Workbook book = new Workbook(path + "exmaple.xlsx");
// Accessing the newly added comment
com.aspose.cells.Comment comment = book.getWorksheets().get(0).getComments().get(0);
// change background color
Shape shape = comment.getCommentShape();
shape.getFill().getSolidFill().setColor(Color.getRed());
// Save the Excel file
book.save(path + "result.xlsx");

执行上述代码,你将得到一个输出文件

如何在Excel中评论中插入图片或图像

微软Excel允许用户在很大程度上自定义电子表格的外观。甚至可以向评论中添加背景图片。添加背景图片可以是一种美学选择,也可以用于加强品牌形象。

下面的示例代码使用Aspose.Cells API从头开始创建一个XLSX文件,并向单元A1添加具有图片背景的评论。

// 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();
// Instantiate a Workbook
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);
// Save the workbook
String resDataDir = dataDir + "commentwithpicture1.out.xlsx";
workbook.save(resDataDir);