管理评论和注释
Contents
[
Hide
]
介绍
注释用于向单元格添加附加信息。 Aspose.Cells 提供了两种向单元格添加注释的方法。第一种是在设计器文件中手动创建注释。然后使用Aspose.Cells导入这些评论。第二个是在运行时使用Aspose.Cells API添加评论。本主题讨论使用 Aspose.Cells API 向单元格添加注释。还将解释格式化注释。
添加评论
通过调用注释收藏的添加方法(封装在工作表目的)。新的评论对象可以从注释通过传递评论索引进行收集。访问后评论对象,使用评论对象的笔记财产。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
评论格式
还可以通过配置评论的高度、宽度和字体设置来格式化评论的外观。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 中,这是通过执行以下步骤完成的。 (他们假设您已经添加了单元格评论。)
- 右键单击包含注释的单元格。
- 选择显示/隐藏评论, 并清除评论中的任何文本。
- 单击评论的边框以将其选中。
- 选择格式, 然后评论.
- 在颜色和线条选项卡,展开颜色列表。
- 点击填充效果.
- 在图片选项卡,单击选择图片.
- 找到并选择图片。
- 点击好的直到所有对话框都关闭。
Aspose.Cells也提供了这个功能。下面是一个代码示例,它从头开始创建一个 XLSX 文件,向单元格“A1”添加注释,并将图片集作为其背景。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |