管理 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");