Share via


方法 : サーバー上のブックからキャッシュされたデータを取得する

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

Microsoft Office のバージョン

  • Excel 2007

  • Excel 2003

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

Excel を実行せずに、ドキュメント レベルの Visual Studio Tools for Office プロジェクトの一部である Microsoft Office Excel ブックのキャッシュからデータを取得できます。これによって、サーバー上に格納されている Excel ブックからデータを取得できます。

データを取得するコードは、ASP.NET Web ページ、コンソール アプリケーション、Windows フォーム アプリケーションなど、操作中のドキュメントに関連付けられている Visual Studio Tools for Office プロジェクト アセンブリの外にある必要があります。

このトピックのコード例の詳細な使用手順については、「チュートリアル : サーバー上のブックからのキャッシュされたデータの取得」を参照してください。

使用例

次のコード例は、AdventureWorksLTDataSet という名前の型指定されたデータセットのインスタンスを最初に作成します。次に、ServerDocument クラスの CachedData プロパティを使用して、Excel ブックに既にキャッシュされている同じ型指定されたデータセットの値が設定されたインスタンスにアクセスし、キャッシュされたデータセットからローカル データセットにデータを読み込みます。

Dim productDataSet As New AdventureWorksDataSet.AdventureWorksLTDataSet()
Dim workbookPath As String = System.Environment.GetFolderPath( _
    Environment.SpecialFolder.MyDocuments) & _
    "\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx"
Dim serverDocument1 As ServerDocument = Nothing

Try
    serverDocument1 = New ServerDocument(workbookPath)
    Dim dataHostItem1 As CachedDataHostItem = _
        serverDocument1.CachedData.HostItems("AdventureWorksReport.Sheet1")
    Dim dataItem1 As CachedDataItem = dataHostItem1.CachedData("AdventureWorksLTDataSet")

    If dataItem1 IsNot Nothing Then
        Console.WriteLine("Before reading data from the cache dataset, the local dataset has " & _
            "{0} rows.", productDataSet.Product.Rows.Count.ToString())

        ' Read the cached data from the worksheet dataset into the local dataset.
        Dim schemaReader As New System.IO.StringReader(dataItem1.Schema)
        Dim xmlReader As New System.IO.StringReader(dataItem1.Xml)
        productDataSet.ReadXmlSchema(schemaReader)
        productDataSet.ReadXml(xmlReader)

        Console.WriteLine("After reading data from the cache dataset, the local dataset has " & _
            "{0} rows.", productDataSet.Product.Rows.Count.ToString())
    Else
        Console.WriteLine("The data object is not found in the data cache.")
    End If
Catch ex As System.IO.FileNotFoundException
    Console.WriteLine("The specified workbook does not exist.")
Catch ex As System.Xml.XmlException
    Console.WriteLine("The data object has invalid XML information.")
Finally
    If Not (serverDocument1 Is Nothing) Then
        serverDocument1.Close()
    End If
    Console.WriteLine(vbLf & vbLf & "Press Enter to close the application.")
    Console.ReadLine()
End Try
AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
    new AdventureWorksDataSet.AdventureWorksLTDataSet();
string workbookPath = System.Environment.GetFolderPath(
    Environment.SpecialFolder.MyDocuments) +
    @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
ServerDocument serverDocument1 = null;

try
{
    serverDocument1 = new ServerDocument(workbookPath);
    CachedDataHostItem dataHostItem1 =
        serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
    CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

    if (dataItem1 != null)
    {
        Console.WriteLine("Before reading data from the cache dataset, the local dataset has " +
            "{0} rows.", productDataSet.Product.Rows.Count.ToString());

        // Read the cached data from the worksheet dataset into the local dataset.
        System.IO.StringReader schemaReader = new System.IO.StringReader(dataItem1.Schema);
        System.IO.StringReader xmlReader = new System.IO.StringReader(dataItem1.Xml);
        productDataSet.ReadXmlSchema(schemaReader);
        productDataSet.ReadXml(xmlReader);

        Console.WriteLine("After reading data from the cache dataset, the local dataset has " +
            "{0} rows.", productDataSet.Product.Rows.Count.ToString());
    }
    else
    {
        Console.WriteLine("The data object is not found in the data cache.");
    }
}
catch (System.IO.FileNotFoundException)
{
    Console.WriteLine("The specified workbook does not exist.");
}
catch (System.Xml.XmlException)
{
    Console.WriteLine("The data object has invalid XML information.");
}
finally
{
    if (serverDocument1 != null)
    {
        serverDocument1.Close();
    }

    Console.WriteLine("\n\nPress Enter to close the application.");
    Console.ReadLine();
}

コードのコンパイル方法

このトピックのコード例は、次のアプリケーションと組み合わせて使用するよう設計されています。

  • 型指定されたデータセットを定義するクラス ライブラリ プロジェクトにアクセスするコンソール アプリケーション。コードは、コンソール アプリケーションで実行されます。

  • Excel 2003 または Excel 2007 のドキュメント レベルのカスタマイズの一部である Excel ブック。ブックは、データを含む AdventureWorksLTDataSet という名前のキャッシュされたデータセットを持ちます。

コードの詳細な使用手順については、「チュートリアル : サーバー上のブックからのキャッシュされたデータの取得」を参照してください。

参照

処理手順

チュートリアル : サーバー上のブックからのキャッシュされたデータの取得

方法 : オフラインで使用するデータまたはサーバー上で使用するデータをキャッシュする

方法 : サーバー上のブックでキャッシュされたデータを変更する

概念

サーバー上のドキュメント内のデータへのアクセス

ドキュメント レベルのカスタマイズのデータ モデル