管理注释和备注

介绍

注释用于向单元格添加额外信息。Aspose.Cells提供两种方法来向单元格添加注释。第一种是手动在设计文件中创建注释,然后使用Aspose.Cells导入这些注释。第二种是在运行时使用Aspose.Cells API添加注释。本主题讨论使用Aspose.Cells API向单元格添加注释。还将介绍注释的格式设置。

添加注释

通过调用Comments集合的Add方法(封装在Worksheet对象中)向单元格添加注释。可以通过传递注释索引从Comments集合中访问新的Comment对象。在访问Comment对象之后,使用Comment对象的Note属性自定义注释。

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

注释格式设置

还可以通过配置其高度、宽度和字体设置来格式化注释的外观。

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

向注释添加图像

在Microsoft Excel 2007中,还可以将图像添加为单元格注释的背景。在Excel 2007中,可以通过以下步骤完成这一操作。(假设您已经添加了单元格注释。)

1.右键单元格,然后选择显示/隐藏注释,清除注释中的任何文本。 1.点击注释的边框进行选择。 1.选择格式,然后选择注释。 1.在颜色和线条选项卡上,展开颜色列表。 1.单击填充效果。 1.单击图片选项卡。 1.在图片选项卡上,单击选择图片。 1.定位并选择图片。

  1. 点击 确定 直到所有对话框都关闭。

Aspose.Cells 也提供了这个功能。下面是一个代码示例,从头开始创建一个 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();
// 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");

高级主题