Manage Microsoft® Excel File Metadata via .NET

View, add, update, remove or extract built-in and custom Excel file properties using server side .NET APIs.

 

.NET Excel API supports the management of system-defined (built-in) properties such as title, author name, document statistics etc as well as user-defined (custom) properties in the form of name-value pair. There is Workbook class to load the files, and WorksheetCollection deals with collection of worksheets as well as Worksheet class for representing single worksheet. Along with these classes, BuiltInDocumentProperties, CustomDocumentProperties makes the process simple for metadata management.

Managing Built-in Properties

For managing system-defined properties, API provides BuiltInDocumentProperties , and programmers can easily access a built-in property and update its value. Depending on application requirement, developers can use the index or property name from the DocumentPropertyCollection .

C# Code to Manage Builtin Properties
//Create workbook object.
Workbook wb = new Workbook();
//Access system defined document property collection.
Aspose.Cells.Properties.BuiltInDocumentPropertyCollection sdmd = wb.BuiltInDocumentProperties;
//Set the language of the Excel document.
sdmd.Language = "German, French";
//Save the workbook in xlsx format.
wb.Save(outputDir + "system-defined-properties-updated.xlsx", SaveFormat.Xlsx);
 

Managing Custom Defined Properties

For managing user-defined properties, API provides CustomDocumentProperties , and developers can easily access already added properties as well as add new properties. In order to add custom properties, Add method of CustomDocumentPropertyCollection class adds the property and returns a reference for the new property as an Properties.DocumentProperty object. DocumentProperty class is used to retrieve the name, value, and type of the document property as DocumentProperty.Name , DocumentProperty.Value , DocumentProperty.Type that returns one of the PropertyType enumeration values.

C# Code to Add Metadata in Excel File
// string dataDir = "he path to the documents directory."
// Instantiate a Workbook object
// Open an Excel file
Workbook wkb = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = wkb.Worksheets.CustomDocumentProperties;
// Adding a custom document property to the Excel file
Aspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose");
// Add link to content.
customProperties.AddLinkToContent("Owner", "MyRange");
// way to access custom property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"];
// Saving resultant spreadsheet
wkb.Save(dataDir + "out_sample-document-properties.xlsx");
C# Code to Remove Custom Property in Excel File
//string dataDir = "The path to the documents directory";
// Instantiate a Workbook object
// Open an Excel file
Workbook wkb = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = wkb.Worksheets.CustomDocumentProperties;
// Removing a custom document property
customProperties.Remove("Publisher");
// Save the file
wkb.Save(dataDir + "out_sample-document-properties.xlsx");