管理 Microsoft® Excel 文件元数据 via Java
使用服务器端 Java API 查看、添加、更新、删除或提取自定义和内置 Excel 文件属性。
Java Excel API 支持管理内置(系统定义)属性,例如标题、作者姓名、文档统计信息等,以及名称/值对形式的自定义(用户定义)属性。有 作业本类 加载文件,以及 工作表集合 处理工作表的收集以及 工作表类 用于表示单个工作表。对于访问内置和自定义属性,BuiltInDocumentProperties、CustomDocumentProperties 使元数据管理过程变得简单。
管理系统定义的属性
为了管理内置属性,API 提供 内置文档属性 ,程序员可以轻松访问内置属性并更新其值。根据应用程序的要求,开发人员可以使用索引或属性名称 文档属性集合 .
Java 管理系统定义属性的代码
//Create workbook object. | |
Workbook wb = new Workbook(); | |
//Access system defined document property collection. | |
BuiltInDocumentPropertyCollection sdpc = wb.getBuiltInDocumentProperties(); | |
//Set the language of the Excel file. | |
sdpc.setLanguage("German, French"); | |
//Save the workbook. | |
wb.save(outputDir + "updated-builtin-document-properties.xlsx", SaveFormat.XLSX); |
添加和删除自定义元数据
为了处理自定义属性,API 提供 自定义文档属性 ,开发人员可以轻松访问现有属性以及使用以下命令添加新属性[添加方法]( https://reference.aspose.com/cells/java/com.aspose.cells/customdocumentpropertycollection#add(java.lang.String,%20boolean) ) 的 自定义文档属性集合 类添加属性并返回新属性的引用作为 属性.文档属性 目的。 DocumentProperty 类用于检索文档属性的名称、值和类型,如下所示 文档属性.名称 , 文档属性.值 , 文档属性.类型 返回其中之一 财产种类 枚举值。
Java 在 Excel 文件中添加元数据的代码
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook wkb = new Workbook(dataDir + "sample.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
CustomDocumentPropertyCollection customProperties = wkb.getWorksheets().getCustomDocumentProperties(); | |
// Adding a custom document property to the Excel file | |
DocumentProperty publisher = customProperties.add("Publisher", "Aspose"); | |
// 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(); | |
// save the workbook |
Java 删除 Excel 文件中的自定义属性的代码
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook wkb = new Workbook(dataDir + "sample.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
DocumentPropertyCollection customProperties = wkb.getWorksheets().getCustomDocumentProperties(); | |
// Removing a custom document property | |
customProperties.remove("Publisher"); |