Export (0) Print
Expand All

How to: Retrieve a dictionary of all named ranges in a spreadsheet document (Open XML SDK)

Last modified: March 22, 2013

Applies to: Office 2013 | Open XML

In this article
GetDefinedNames Method
Calling the Sample Method
How the Code Works
Retrieving the Defined Names
Sample Code

This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically retrieve a dictionary that contains the names and ranges of all defined names in an Microsoft Excel 2010 or Microsoft Excel 2013 workbook. It contains an example GetDefinedNames method to illustrate this task.

To use the sample code in this topic, you must install the Open XML SDK 2.5. You must explicitly reference the following assemblies in your project:

  • WindowsBase

  • DocumentFormat.OpenXml (Installed by the Open XML SDK)

You must also use the following using directives or Imports statements to compile the code in this topic.

using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

The GetDefinedNames procedure accepts a single parameter that indicates the name of the document from which to retrieve the defined names. The procedure returns an Dictionary instance that contains information about the defined names within the specified workbook, which may be empty if there are no defined names.

public static Dictionary<String, String>
    GetDefinedNames(String fileName)

The method examines the workbook that you specify, looking for the part that contains defined names. If it exists, the code iterates through all the contents of the part, adding the name and value for each defined name to the returned dictionary.

To call the sample method, pass a string that contains the name of the file from which to retrieve the defined names. The following code example passes a string that contains the name of the file from which to retrieve the defined names and iterates through the returned dictionary, and displays the key and value from each item.

var result = 
    GetDefinedNames(@"C:\Users\Public\Documents\definednames.xlsx");
foreach (var dn in result)
    Console.WriteLine("{0} {1}", dn.Key, dn.Value);

The code starts by creating a variable named returnValue that the method will return before it exits.

// Given a workbook name, return a dictionary of defined names.
// The pairs include the range name and a string representing the range.
var returnValue = new Dictionary<String, String>();
    // Code removed here…
return returnValue;

The code continues by opening the spreadsheet document, using the Open method and indicating that the document should be open for read-only access (the final false parameter). Given the open workbook, the code uses the WorkbookPart property to navigate to the main workbook part. The code stores this reference in a variable named wbPart.

// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document =
    SpreadsheetDocument.Open(fileName, false))
{
    // Retrieve a reference to the workbook part.
    var wbPart = document.WorkbookPart;
    // Code removed here.
}

Given the workbook part, the next step is simple. The code uses the Workbook property of the workbook part to retrieve a reference to the content of the workbook, and then retrieves the DefinedNames collection provided by the Open XML SDK 2.5. This property returns a collection of all of the defined names that are contained within the workbook. If the property returns a non-null value, the code then iterates through the collection, retrieving information about each named part and adding the key (name) and value (range description) to the dictionary for each defined name.

// Retrieve a reference to the defined names collection.
DefinedNames definedNames = wbPart.Workbook.DefinedNames;

// If there are defined names, add them to the dictionary.
if (definedNames != null)
{
    foreach (DefinedName dn in definedNames)
        returnValue.Add(dn.Name.Value, dn.Text);
}

The following is the complete GetDefinedNames code sample in C# and Visual Basic.

public static Dictionary<String, String>
    GetDefinedNames(String fileName)
{
    // Given a workbook name, return a dictionary of defined names.
    // The pairs include the range name and a string representing the range.
    var returnValue = new Dictionary<String, String>();
    
    // Open the spreadsheet document for read-only access.
    using (SpreadsheetDocument document =
        SpreadsheetDocument.Open(fileName, false))
    {
        // Retrieve a reference to the workbook part.
        var wbPart = document.WorkbookPart;
        
        // Retrieve a reference to the defined names collection.
        DefinedNames definedNames = wbPart.Workbook.DefinedNames;

        // If there are defined names, add them to the dictionary.
        if (definedNames != null)
        {
            foreach (DefinedName dn in definedNames)
                returnValue.Add(dn.Name.Value, dn.Text);
        }
    }
    return returnValue;
}

Contribute to this article

Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.

Show:
© 2014 Microsoft