Read Metadata from XLSX using C#

In modern software development, the ability to extract and manipulate metadata from files is crucial for various applications. Metadata, which includes information such as author details, creation dates, and document properties, provides valuable insights into files and aids in efficient file management. When it comes to Excel files in the .xlsx format, accessing metadata programmatically can be particularly useful. In this guide, we’ll explore how to read metadata from XLSX using C# programming language. Here are the key steps and a code example demonstrating how to read metadata of XLSX using C#.

Steps to Read Metadata from XLSX using C#

  1. Configure your IDE to employ GroupDocs.Metadata for .NET for extracting metadata from XLSX files
  2. Instantiate the Metadata class by providing the file path of the XLSX file as an argument to its constructor
  3. Develop a predicate to examine all acquired metadata attributes
  4. Specify a predicate for the Metadata.FindProperties method
  5. Iterate over the obtained properties

XLSX is a popular file format for spreadsheets, primarily used by Microsoft Excel. Apart from the actual data within the spreadsheet, XLSX files contain metadata that describes various aspects of the file. This metadata can include details such as the document’s title, author, creation date, modification date, and more. Accessing this metadata programmatically allows developers to automate tasks, perform analysis, or enhance user experiences within their applications. Following the provided instructions will work on popular systems like Windows, macOS, and Linux, as long as you have .NET installed. You don’t need to install any extra software to extract metadata of XLSX in C#. Once you’ve set up the suggested library and changed the file paths as needed, adding the following code to your projects should go smoothly without any problems.

Code to Read Metadata from XLSX using C#

using GroupDocs.Metadata;
using GroupDocs.Metadata.Common;
using GroupDocs.Metadata.Tagging;
using System.Text.RegularExpressions;
namespace ReadMetadataFromXLSXUsingCSharp
{
internal class Program
{
static void Main(string[] args)
{
// Set License to avoid the limitations of Metadata library
License lic = new License();
lic.SetLicense(@"GroupDocs.Metadata.lic");
// Pass absolute or relative path of document to Metadata's constructor
using (Metadata metadata = new Metadata(@"input.xlsx"))
{
if (metadata.FileFormat != FileFormat.Unknown && !metadata.GetDocumentInfo().IsEncrypted)
{
Console.WriteLine();
// Fetch all metadata properties that fall into a particular category
var properties = metadata.FindProperties(p => p.Tags.Any(t => t.Category == Tags.Content));
Console.WriteLine("The metadata properties describing some characteristics of the file content: title, keywords, language, etc.");
foreach (var property in properties)
{
Console.WriteLine("{0} = {1}", property.Name, property.Value);
}
// Fetch all properties having a specific type and value
var year = DateTime.Today.Year;
properties = metadata.FindProperties(p => p.Value.Type == MetadataPropertyType.DateTime &&
p.Value.ToStruct(DateTime.MinValue).Year == year);
Console.WriteLine("All datetime properties with the year value equal to the current year");
foreach (var property in properties)
{
Console.WriteLine("{0} = {1}", property.Name, property.Value);
}
// Fetch all properties whose names match the specified regex
const string pattern = "^author|company|(.+date.*)$";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
properties = metadata.FindProperties(p => regex.IsMatch(p.Name));
Console.WriteLine("All properties whose names match the following regex: {0}", pattern);
foreach (var property in properties)
{
Console.WriteLine("{0} = {1}", property.Name, property.Value);
}
}
}
}
}
}

Reading metadata from XLSX files using C# is a straightforward process with the metadata library. By leveraging this library, developers can access valuable information embedded within XLSX files and incorporate it into their applications. Whether it’s automating document management tasks, performing data analysis, or enhancing user experiences, accessing XLSX metadata programmatically opens up a world of possibilities for C# developers. Well done! You’ve now mastered the skill of how to get metadata of XLSX in C#.

In our previous discussion, we provided a comprehensive tutorial on how to extract metadata from DOCX files using C#. For a more thorough understanding of this topic, we recommend referring to our detailed guide on how to read metadata from DOCX using C#.

 English