DEV Community

Cover image for How to Target Excel Metadata in C# for Smarter Document Management
Phinter Atieno for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

How to Target Excel Metadata in C# for Smarter Document Management

TL;DR: Struggling to make your Excel files searchable and compliant? This guide shows how to target Excel metadata in C# using Syncfusion XlsIO, automating built-in and custom properties with clean, developer-friendly code.

Excel files power enterprise workflows, financial reports, invoices, and compliance sheets, but without consistent metadata, they’re hard to search, govern, or route through retention policies.

In this guide, you’ll learn how to automate Excel built-in and custom document properties in C# using Essential XlsIO, demonstrated through a real-world invoice scenario. This approach improves searchability, supports compliance, and eliminates manual errors.

Why Syncfusion® .NET Excel library?

The XlsIO is a powerful .NET Excel library that lets you create, read, and edit Excel files in C# without Microsoft Office. It supports advanced features like formulas, charts, pivot tables, conditional formatting, and more, making it ideal for enterprise-grade automation.

Prerequisites

Before you begin, make sure you have the following installed:

Steps to add document properties using C

Step 1: Create a .NET application

Start by creating a new .NET Core console app in Visual Studio.

Step 2: Install Essential XlsIO

Then, install the latest Syncfusion.XlsIO.NET.Core NuGet package from NuGet Gallery.

Step 3: Add the namespaces

Add the namespaces below to the Program.cs file to use the Syncfusion Excel library.

using Syncfusion.XlsIO;
using Syncfusion.Drawing;
Enter fullscreen mode Exit fullscreen mode

Step 4: Invoice generation in Excel

Next, create a method to generate an invoice in an Excel workbook. This invoice will include customer details, itemized billing, and totals, which we’ll use to populate document properties.

public void AddInvoiceDetails(IWorksheet worksheet)
{
    //Disable gridlines in the worksheet
    worksheet.IsGridLinesVisible = false;

    //Enter text to the cell A1 and apply formatting.
    worksheet.Range["A1:D1"].Merge();
    worksheet.Range["A1"].Text = "SALES INVOICE";
    worksheet.Range["A1"].CellStyle.Font.Bold = true;
    worksheet.Range["A1"].CellStyle.Font.RGBColor = Color.FromArgb(42, 118, 189);
    worksheet.Range["A1"].CellStyle.Font.Size = 35;
    ...
    ...

    // Unit Prices (in USD)
    worksheet.Range["D16"].Number = 15;  
    worksheet.Range["D17"].Number = 35;  
    worksheet.Range["D18"].Number = 12;  
    worksheet.Range["D19"].Number = 45;
    worksheet.Range["D20"].Number = 30;

    worksheet.Range["D23"].Text = "Total";

    //Apply number format
    worksheet.Range["D16:E22"].NumberFormat = "$0.00";
    worksheet.Range["E23"].NumberFormat = "$0.00";

    //Apply incremental formula for column Amount by multiplying Qty and UnitPrice
    worksheet.Application.EnableIncrementalFormula = true;
    worksheet.Range["E16:E20"].Formula = "=C16*D16";

    //Formula for Sum the total
    worksheet.Range["E23"].Formula = "=SUM(E16:E22)";

    //Apply borders
    worksheet.Range["A16:E22"].CellStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
...
... worksheet.Range["A23:E23"].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].Color = ExcelKnownColors.Black;

    //Apply font setting for cells with product details
    worksheet.Range["A3:E23"].CellStyle.Font.FontName = "Arial";
    worksheet.Range["A3:E23"].CellStyle.Font.Size = 10;
    worksheet.Range["A15:E15"].CellStyle.Font.Color = ExcelKnownColors.White;
    worksheet.Range["A15:E15"].CellStyle.Font.Bold = true;
    worksheet.Range["D23:E23"].CellStyle.Font.Bold = true;

    //Apply cell color
    worksheet.Range["A15:E15"].CellStyle.Color = Color.FromArgb(42, 118, 189);

    //Apply alignment to cells with product details
    worksheet.Range["A15"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignLeft;
    ...
    ...

    //Apply row height and column width to look good
    worksheet.Range["A1"].ColumnWidth = 36;
    ...
    ...
    worksheet.Range["A15:A23"].RowHeight = 18;
}
Enter fullscreen mode Exit fullscreen mode

For the full implementation of the AddInvoiceDetails method, check the GitHub sample linked below.

Step 5: Add document metadata

Refer to the following code example to add built-in and custom document properties to the invoice workbook. It utilizes invoice-related details for some properties to make them searchable.

public static void ApplyDocumentProperties(IWorkbook workbook)
{
    IWorksheet worksheet = workbook.Worksheets[0];

    // Read key invoice details from the worksheet
    int invoiceNumber = (int)worksheet.Range["D6"].Number;         
    string invoiceDateText = worksheet.Range["E6"].DisplayText;    
    int customerId = (int)worksheet.Range["D8"].Number;            
    string terms = worksheet.Range["E8"].DisplayText;              
    string customerName = worksheet.Range["A8"].DisplayText;       
    string customerCompany = worksheet.Range["A9"].DisplayText;    
    DateTime invoiceDate = DateTime.Now;

    // Add the document properties for the invoice 
    IBuiltInDocumentProperties builtInProperties = workbook.BuiltInDocumentProperties;
    builtInProperties.Title = $"Invoice #{invoiceNumber}";
    builtInProperties.Author = "Jim Halper";
    builtInProperties.Subject = $"Invoice for {customerName} ({customerCompany})";
    builtInProperties.Keywords = $"invoice;billing;customer:{customerId};terms:{terms}";
    builtInProperties.Company = "Great Lake Enterprises";
    builtInProperties.Category = "Finance/Billing";
    builtInProperties.Comments = $"Issued {invoiceDate:yyyy-MM-dd}";

    // Add the custom document properties for the invoice
    var customProperties = workbook.CustomDocumentProperties;
    customProperties["InvoiceNumber"].Value = invoiceNumber;
    customProperties["InvoiceDate"].Text = invoiceDate.ToString("yyyy-MM-dd");
    customProperties["CustomerId"].Value = customerId;
    customProperties["CustomerName"].Text = customerName;
    customProperties["CustomerCompany"].Text = customerCompany;
    customProperties["Currency"].Text = "USD";
    customProperties["PaymentStatus"].Text = "Completed";
    customProperties["Confidential"].Value = true;
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Save the Excel document

Finally, save the Excel file after generating the invoice and applying metadata. This ensures all properties are embedded and ready for indexing or governance workflows.

static void Main(string[] args)
{
    //Create an instance of ExcelEngine
    using (ExcelEngine excelEngine = new ExcelEngine())
    {
        IApplication application = excelEngine.Excel;
        application.DefaultVersion = ExcelVersion.Xlsx;

        //Create a workbook
        IWorkbook workbook = application.Workbooks.Create(1);
        IWorksheet worksheet = workbook.Worksheets[0];

        //Generate Invoice
        AddInvoiceDetails(worksheet);

        //Apply built-in and custom document properties
        ApplyDocumentProperties(workbook);

        workbook.SaveAs(Path.GetFullPath("DocumentProperties.xlsx"));
    }
}
Enter fullscreen mode Exit fullscreen mode

The screenshots given below show the generated invoice and its associated built-in and custom properties in Excel.

Generated Excel invoice


Generated Excel invoice

Built-in and custom properties in Excel


Built-in and custom properties in Excel

Why this matters: Key use cases

  • Quickly locate files in SharePoint or Azure AI Search using metadata tags.
  • Apply retention and security policies based on metadata fields.
  • Trigger automated actions in CI/CD or RPA workflows using metadata values.
  • Enable filtering and faceting in enterprise search platforms.
  • Tag reports with project or department info for analytics dashboards.
  • Mark documents as Confidential or Public for access control.
  • Track revisions and authorship through built-in properties.
  • Preserve metadata when exporting Excel to PDF, HTML, or JSON.

Reference

For more details, refer to the complete code examples from GitHub.

Conclusion

Thanks for reading! By automating Excel metadata in C# with Syncfusion .NET Excel Library, you make documents searchable, governable, and ready for enterprise workflows. This approach enhances compliance, improves discoverability in platforms like SharePoint and Azure Search, and eliminates manual errors..

Explore our getting started guide and try our online demos for advanced features and additional code examples. The library also supports exporting Excel data to PDF, images, data tables, HTML, CSV, TSV, collections of objects, ODS, and JSON.

You can contact us through our support forum, support portal, or feedback portal for queries. We are always happy to assist you!

Related Blogs

This article was originally published at Syncfusion.com.

Top comments (0)