diff --git a/docs/spreadsheet/how-to-retrieve-a-dictionary-of-all-named-ranges-in-a-spreadsheet.md b/docs/spreadsheet/how-to-retrieve-a-dictionary-of-all-named-ranges-in-a-spreadsheet.md index c16e418a..33182af6 100644 --- a/docs/spreadsheet/how-to-retrieve-a-dictionary-of-all-named-ranges-in-a-spreadsheet.md +++ b/docs/spreadsheet/how-to-retrieve-a-dictionary-of-all-named-ranges-in-a-spreadsheet.md @@ -23,112 +23,25 @@ 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. - - ## GetDefinedNames Method -The **GetDefinedNames** procedure accepts a +The **GetDefinedNames** method accepts a single parameter that indicates the name of the document from which to -retrieve the defined names. The procedure returns an +retrieve the defined names. The method returns an [Dictionary](https://msdn.microsoft.com/library/xfhwa508.aspx) instance that contains information about the defined names within the specified workbook, which may be empty if there are no defined names. -### [C#](#tab/cs-0) -```csharp - public static Dictionary - GetDefinedNames(String fileName) -``` - -### [Visual Basic](#tab/vb-0) -```vb - Public Function GetDefinedNames( - ByVal fileName As String) As Dictionary(Of String, String) -``` -*** - - -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. - -## Calling the Sample Method - -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. - -### [C#](#tab/cs-1) -```csharp - var result = - GetDefinedNames(@"C:\Users\Public\Documents\definednames.xlsx"); - foreach (var dn in result) - Console.WriteLine("{0} {1}", dn.Key, dn.Value); -``` - -### [Visual Basic](#tab/vb-1) -```vb - Dim result = - GetDefinedNames("C:\Users\Public\Documents\definednames.xlsx") - For Each dn In result - Console.WriteLine("{0}: {1}", dn.Key, dn.Value) -``` -*** - - ## How the Code Works -The code starts by creating a variable named **returnValue** that the method will return before it exits. - -### [C#](#tab/cs-2) -```csharp - // 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(); - // Code removed here… - return returnValue; -``` - -### [Visual Basic](#tab/vb-2) -```vb - ' Given a workbook name, return a dictionary of defined names. - ' The pairs include the range name and a string representing the range. - Dim returnValue As New Dictionary(Of 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**. +The code opens the spreadsheet document, using the **Open** method, indicating that the +document should be open for read-only access with 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**. ### [C#](#tab/cs-3) -```csharp - // 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. - } -``` +[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet1)] ### [Visual Basic](#tab/vb-3) -```vb - ' Open the spreadsheet document for read-only access. - Using document As SpreadsheetDocument = - SpreadsheetDocument.Open(fileName, False) - - ' Retrieve a reference to the workbook part. - Dim wbPart As WorkbookPart = document.WorkbookPart - ' Code removed here… - End Using -``` +[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet1)] *** @@ -139,30 +52,10 @@ Given the workbook part, the next step is simple. The code uses 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. ### [C#](#tab/cs-4) -```csharp - // 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); - } -``` +[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet2)] ### [Visual Basic](#tab/vb-4) -```vb - ' Retrieve a reference to the defined names collection. - Dim definedNames As DefinedNames = wbPart.Workbook.DefinedNames - - ' If there are defined names, add them to the dictionary. - If definedNames IsNot Nothing Then - For Each dn As DefinedName In definedNames - returnValue.Add(dn.Name.Value, dn.Text) - Next - End If -``` +[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet2)] *** @@ -171,10 +64,10 @@ defined names that are contained within the workbook. If the property returns a The following is the complete **GetDefinedNames** code sample in C\# and Visual Basic. ### [C#](#tab/cs) -[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs)] +[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet0)] ### [Visual Basic](#tab/vb) -[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb)] +[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet0)] ## See also diff --git a/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs b/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs index 3ae2c819..d3677413 100644 --- a/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs +++ b/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs @@ -1,24 +1,25 @@ +// using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using System; using System.Collections.Generic; -GetDefinedNames(args[0]); - -static Dictionary - GetDefinedNames(String fileName) +static DictionaryGetDefinedNames(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(); + // // Open the spreadsheet document for read-only access. - using (SpreadsheetDocument document = - SpreadsheetDocument.Open(fileName, false)) + 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; @@ -33,12 +34,18 @@ static Dictionary } } } - } - foreach (var pair in returnValue) - { - Console.WriteLine("{0} {1}", pair.Key, pair.Value); + // } return returnValue; -} \ No newline at end of file +} + +// + +var definedNames = GetDefinedNames(args[0]); + +foreach (var pair in definedNames) +{ + Console.WriteLine("Name: {0} Value: {1}", pair.Key, pair.Value); +} diff --git a/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb b/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb index 49c57887..d634aaa2 100644 --- a/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb +++ b/samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb @@ -1,26 +1,33 @@ +' Imports DocumentFormat.OpenXml.Packaging Imports DocumentFormat.OpenXml.Spreadsheet Module Program Sub Main(args As String()) - End Sub + Dim definedNames = GetDefinedNames(args(0)) + For Each definedName In definedNames + Console.WriteLine("Name: {0} Value: {1}", definedName.Key, definedName.Value) + Next + End Sub - Public Function GetDefinedNames( - ByVal fileName As String) As Dictionary(Of String, String) + Public Function GetDefinedNames(ByVal fileName As String) As Dictionary(Of String, String) ' Given a workbook name, return a dictionary of defined names. ' The pairs include the range name and a string representing the range. Dim returnValue As New Dictionary(Of String, String) + ' ' Open the spreadsheet document for read-only access. - Using document As SpreadsheetDocument = - SpreadsheetDocument.Open(fileName, False) + Using document As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False) ' Retrieve a reference to the workbook part. Dim wbPart As WorkbookPart = document.WorkbookPart + ' + + ' ' Retrieve a reference to the defined names collection. Dim definedNames As DefinedNames = wbPart.Workbook.DefinedNames @@ -30,7 +37,11 @@ Module Program returnValue.Add(dn.Name.Value, dn.Text) Next End If + + ' End Using Return returnValue End Function -End Module \ No newline at end of file +End Module + +'