管理文档属性

介绍

Microsoft Excel提供了向电子表格文件添加属性的功能。这些文档属性提供有用信息,分为以下2类。

  • 系统定义(内置)属性:内置属性包含有关文档的一般信息,如文档标题、作者姓名、文档统计信息等。
  • 用户定义(自定义)属性:最终用户以名称-值对的形式定义的自定义属性。

使用Microsoft Excel管理文档属性

Microsoft Excel允许以所见即所得的方式管理Excel文件的文档属性。请按以下步骤在Excel 2016中打开“属性”对话框。

  1. 文件菜单中选择信息
选择信息菜单
todo:image_alt_text
  1. 点击属性标题并选择"高级属性"。
单击高级属性选择
todo:image_alt_text
  1. 管理文件的文档属性。
属性对话框
todo:image_alt_text
在属性对话框中,有不同的选项卡,如常规、摘要、统计、内容和自定义。每个选项卡都可以帮助配置文件相关的不同信息。自定义选项卡用于管理自定义属性。

使用Aspose.Cells管理文档属性

开发人员可以使用Aspose.Cells API动态管理文档属性。此功能帮助开发人员存储有用信息,如文件接收时间、处理时间戳等。

访问文档属性

Aspose.Cells API支持内建和自定义文档属性。Aspose.Cells的Workbook类代表Excel文件,类似于Excel文件,Workbook类可以包含多个工作表,每个工作表由Worksheet类表示,而工作表的集合由WorksheetCollection类表示。

使用WorksheetCollection如下访问文件的文档属性。

WorksheetCollection.BuiltInDocumentPropertiesWorksheetCollection.CustomDocumentProperties都返回DocumentPropertyCollection的实例。这个集合包含DocumentProperty对象,每个对象表示单个内置或自定义文档属性。

应用程序要求如何访问属性取决于;即通过从DocumentPropertyCollection中的索引或属性名称来访问属性,如下面的示例所示。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xls");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Accessing a custom document property by using the property name
DocumentProperty customProperty1 = customProperties.get("ContentTypeId");
System.out.println(customProperty1.getName() + " " + customProperty1.getValue());
// Accessing a custom document property by using the property index
DocumentProperty customProperty2 = customProperties.get(0);
System.out.println(customProperty2.getName() + " " + customProperty2.getValue());

DocumentProperty类允许检索文档属性的名称、值和类型:

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xls");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Accessing a custom document property
DocumentProperty customProperty1 = customProperties.get(0);
// Storing the value of the document property as an object
Object objectValue = customProperty1.getValue();
// Accessing a custom document property
DocumentProperty customProperty2 = customProperties.get(1);
// Checking the type of the document property and then storing the value of the
// document property according to that type
if (customProperty2.getType() == PropertyType.STRING)
{
String value = customProperty2.getValue().toString();
}

添加或删除自定义文档属性

正如我们在本主题开头所述的那样,开发人员无法添加或删除内置属性,因为这些属性是系统定义的,但可以添加或删除自定义属性,因为这些是用户定义的。

添加自定义属性

Aspose.Cells API已经为CustomDocumentPropertyCollection类公开了add方法,以便向集合添加自定义属性。add方法为Excel文件添加属性并以DocumentProperty对象的形式返回新文档属性的引用。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Adding a custom document property to the Excel file
DocumentProperty publisher = customProperties.add("Publisher", "Aspose");

配置“链接到内容”自定义属性

要创建与给定范围的内容链接的自定义属性,请调用CustomDocumentPropertyCollection.addLinkToContent方法并传递属性名称和来源。您可以使用DocumentProperty.isLinkedToContent属性检查属性是否配置为链接到内容。此外,还可以使用DocumentProperty类的Source属性获取源范围。

我们在示例中使用了一个简单的模板Microsoft Excel文件。工作簿有一个命名范围标记为MyRange,它指向单元格值。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Add link to content.
customProperties.addLinkToContent("Owner", "MyRange");
// Accessing the custom document property by using the property name
DocumentProperty customProperty1 = customProperties.get("Owner");
// Check whether the property is lined to content
Boolean islinkedtocontent = customProperty1.isLinkedToContent();
// Get the source for the property
String source = customProperty1.getSource();

移除自定义属性

要使用Aspose.Cells删除自定义属性,调用DocumentPropertyCollection.remove方法并传递要移除的文档属性的名称。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Removing a custom document property
customProperties.remove("Publisher");