管理文档属性

介绍

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

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

使用 Microsoft Excel 管理文档属性

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

  1. 来自文件菜单,选择信息.
选择信息菜单
待办事项:图片_替代_文本
  1. 点击特性标题并选择“高级属性”。
单击高级属性选择
待办事项:图片_替代_文本
  1. 管理文件的文档属性。
属性对话框
待办事项:图片_替代_文本
在 Properties 对话框中,有不同的选项卡,如 General、Summary、Statistics、Contents 和 Customs。每个选项卡都有助于配置与文件相关的不同类型的信息。自定义选项卡用于管理自定义属性。

使用 Aspose.Cells 处理文档属性

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

访问文档属性

Aspose.Cells API 支持两种类型的文档属性,内置的和自定义的。 Aspose.Cells'工作簿类表示一个 Excel 文件,并且与 Excel 文件一样,工作簿类可以包含多个工作表,每个工作表由工作表类,而工作表的集合由工作表集合班级。

使用工作表集合访问文件的文档属性,如下所述。

这俩工作表集合.BuiltInDocumentProperties工作表集合.CustomDocumentProperties返回一个实例文档属性集合.这个集合包含文档属性对象,每个对象代表一个内置或自定义的文档属性。

如何访问属性取决于应用程序要求,即;通过使用属性的索引或名称文档属性集合如下例所示。

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

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

// 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暴露了[添加](https://reference.aspose.com/cells/java/com.aspose.cells/customdocumentpropertycollection#add(java.lang.String,%20boolean) 的方法自定义文档属性集合类以便将自定义属性添加到集合中。这[添加](https://reference.aspose.com/cells/java/com.aspose.cells/customdocumentpropertycollection#add(java.lang.String,%20boolean) 方法将属性添加到 Excel 文件并返回新文档属性的引用作为文档属性目的。

// 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 方法并传递属性名称和来源。您可以使用文档属性.isLinkedToContent财产。此外,还可以使用资源的财产文档属性班级。

我们在示例中使用一个简单的模板 Microsoft Excel 文件。工作簿有一个定义的命名范围标记我的范围这是指单元格值。

// 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.删除](https://reference.aspose.com/cells/java/com.aspose.cells/documentpropertycollection#remove(java.lang.String)方法并传递要删除的文档属性的名称。

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