如何在 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中的评论中插入图片或图像

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

下面的示例代码使用从头开始创建一个 XLSX 文件Aspose.CellsAPI ,并在单元格 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);