如何:从服务器上的工作簿中检索缓存的数据

更新: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 工作簿中获取数据。

用于检索数据的代码必须位于与所处理的文档相关联的 Visual Studio Tools for Office 项目程序集之外,例如位于 ASP.NET 网页、控制台应用程序或 Windows 窗体应用程序中。

有关如何使用本主题中的代码示例的分步说明,请参见演练:从服务器上的工作簿中检索缓存的数据

示例

下面的代码示例首先会创建类型化数据集 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。

有关如何使用此代码的分步说明,请参见演练:从服务器上的工作簿中检索缓存的数据

请参见

任务

演练:从服务器上的工作簿中检索缓存的数据

如何:缓存数据以便脱机使用或在服务器上使用

如何:更改服务器上的工作簿中的缓存数据

概念

访问服务器上的文档数据

文档级自定义项中的数据模型