次の方法で共有


スプレッド シート ドキュメントのすべての名前付き範囲の辞書を取得する

適用対象: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

この記事の内容
GetDefinedNames メソッド
サンプル メソッドの呼び出し
コードの動作のしくみ
定義名の取得
サンプル コード

このトピックでは、Open XML SDK 2.0 for Microsoft Office のクラスを使用して、Excel 2007 または Excel 2010 ブック内のすべての定義名の名前と範囲を含んでいる辞書をプログラムによって取得する方法を説明します。このトピックでは、この作業を説明するために例 GetDefinedNames メソッドを使用しています。

このトピックのサンプル コードを使用するには、Open XML SDK 2.0 をインストールする必要があります。プロジェクトで次のアセンブリを明示的に参照する必要があります。

  • WindowsBase

  • DocumentFormat.OpenXml (Open XML SDK によってインストールされます)

このトピックのコードをコンパイルするには、次の using ディレクティブまたは Imports ステートメントも使用する必要があります。

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

GetDefinedNames メソッド

GetDefinedNames プロシージャは、定義名を取得するドキュメントの名前を示すパラメーターを 1 つだけ受け取ります。プロシージャは、指定されたブック内の定義名に関する情報を含んだ Dictionary インスタンスを返します。定義名がない場合は、空のインスタンスが返される可能性があります。

public static Dictionary<String, String>
    GetDefinedNames(String fileName)
Public Function GetDefinedNames(
    ByVal fileName As String) As Dictionary(Of String, String)

このメソッドは、指定されたブックを調べて、定義名を含んでいるパーツを探します。そのようなパーツがある場合、コードによってそのパーツの内容をすべて反復処理し、各定義名の名前と値を辞書に追加して返します。

サンプル メソッドの呼び出し

サンプル メソッドを呼び出すには、定義名を取得するファイルの名前を含んだ文字列を渡します。次のコード例では、定義名を取得するファイルの名前を含んだ文字列を渡し、返す辞書を反復処理して、各アイテムのキーと名前を表示します。

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

コードの動作のしくみ

コードでは、まず、メソッドが終了する前に返す returnValue という名前の変数を作成します。

// 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;
' 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

次に、Open メソッドを使用して、(最後のパラメーターを false にして) 読み取り専用アクセスでスプレッドシート ドキュメントを開きます。開いたブックの WorkbookPart プロパティを使用して、メインのブック パーツに移動します。この参照を 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.
}
' 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

定義名の取得

ブック パーツを取得した後の次の手順は簡単です。ブック パーツの Workbook プロパティを使用してブックの内容への参照を取得し、Open XML SDK 2.0 によって提供される DefinedNames コレクションを取得します。このプロパティは、ブックに含まれるすべての定義名のコレクションを返します。このプロパティから null 以外の値が返された場合は、コレクションを反復処理し、各名前付きパーツに関する情報を取得し、各定義名のキー (名前) と値 (範囲記述) を辞書に追加します。

// 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);
}
' 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

サンプル コード

以下に、C# と Visual Basic の完全な GetDefinedNames コード サンプルを示します。

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;
}
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)
      
        ' 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
        
        ' 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
    End Using
    Return returnValue
End Function

関連項目

参照

Class Library Reference