HOW TO:將資料插入伺服器上的活頁簿中

您不需要執行 Excel,即可將資料插入屬於文件層級 Office 專案一部分的 Microsoft Office Excel 活頁簿快取中。 如此可以讓您將資料插入儲存於伺服器的 Excel 活頁簿中。

**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

用來插入資料的程式碼必須位於與使用中文件相關聯的專案組件之外,例如在主控台或 Windows Form 應用程式中。

如需如何使用本主題程式碼範例的逐步指示,請參閱逐步解說:在伺服器的活頁簿中插入資料

範例

下列程式碼範例會先建立名為 AdventureWorksLTDataSet 之具型別資料集的執行個體,然後使用資料表配接器 (Adapter) 填滿資料集中的 Product 資料表。 接著,程式碼會使用 ServerDocument 類別存取 Excel 活頁簿中快取之相同具型別資料集的執行個體,然後使用 SerializeDataInstance 方法將資料從本機資料集寫入快取的資料集。

Dim productDataSet As New AdventureWorksDataSet.AdventureWorksLTDataSet()
Dim productTableAdapter As _
    New AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter()

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

Try
    productTableAdapter.Fill(productDataSet.Product)
    Console.WriteLine("The local dataset is filled.")

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

    ' Initialize the worksheet dataset with the local dataset.
    If dataItem1 IsNot Nothing Then
        dataItem1.SerializeDataInstance(productDataSet)
        serverDocument1.Save()
        Console.WriteLine("The data is saved to the data cache.")
    Else
        Console.WriteLine("The data object is not found in the data cache.")
    End If
Catch ex As System.Data.SqlClient.SqlException
    Console.WriteLine(ex.Message)
Catch ex As System.IO.FileNotFoundException
    Console.WriteLine("The specified workbook does not exist.")
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();
AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter productTableAdapter =
    new AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter();

string workbookPath = System.Environment.GetFolderPath(
    Environment.SpecialFolder.MyDocuments) +
    @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
ServerDocument serverDocument1 = null;

try
{
    productTableAdapter.Fill(productDataSet.Product);
    Console.WriteLine("The local dataset is filled.");

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

    // Initialize the worksheet dataset with the local dataset.
    if (dataItem1 != null)
    {
        dataItem1.SerializeDataInstance(productDataSet);
        serverDocument1.Save();
        Console.WriteLine("The data is saved to the data cache.");
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("The data object is not found in the data cache.");
    }
}
catch (System.Data.SqlClient.SqlException ex)
{
    Console.WriteLine(ex.Message);
}
catch (System.IO.FileNotFoundException)
{
    Console.WriteLine("The specified workbook does not exist.");
}
finally
{
    if (serverDocument1 != null)
    {
        serverDocument1.Close();
    }

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

編譯程式碼

本主題中的程式碼範例是設計與下列應用程式搭配使用:

  • 主控台應用程式。它能存取類別庫 (Class Library) 專案 (會定義具型別資料集)。 該程式碼會在主控台應用程式中執行。

  • 屬於 Excel 文件層級自訂的 Excel 活頁簿。 此活頁簿具有名為 AdventureWorksLTDataSet 而且包含一些資料的快取資料集。

如需使用該程式碼的逐步指示,請參閱逐步解說:在伺服器的活頁簿中插入資料

請參閱

工作

逐步解說:在伺服器的活頁簿中插入資料

HOW TO:在文件中插入資料而不寫入磁碟

HOW TO:從伺服器的活頁簿中擷取快取資料

HOW TO:變更伺服器活頁簿中的快取資料

概念

存取伺服器文件中的資料

DiffGrams (ADO.NET)