방법: 서버에 있는 통합 문서에 데이터 삽입

업데이트: 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 통합 문서에 데이터를 삽입할 수 있습니다.

데이터를 삽입하는 코드는 콘솔 또는 Windows Forms 응용 프로그램 같이 작업 중인 문서와 연결되어 있는 주 Visual Studio Tools for Office 프로젝트 어셈블리 외부에 있어야 합니다.

이 항목의 코드 예제 사용에 대한 단계별 지침은 연습: 서버의 통합 문서에 데이터 삽입을 참조하십시오.

예제

다음 코드 예제에서는 먼저 AdventureWorksLTDataSet이라는 형식화된 데이터 집합의 인스턴스를 만든 다음 테이블 어댑터를 사용하여 이 데이터 집합의 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();
}

코드 컴파일

이 항목의 코드 예제는 형식화된 데이터 집합을 정의하는 클래스 라이브러리 프로젝트에 액세스할 수 있는 콘솔 응용 프로그램과 Excel 2003 또는 Excel 2007용 문서 수준 사용자 지정의 일부인 Excel 통합 문서에서 사용할 수 있습니다. 코드 사용에 대한 단계별 지침은 연습: 서버의 통합 문서에 데이터 삽입을 참조하십시오.

참고 항목

작업

연습: 서버의 통합 문서에 데이터 삽입

방법: 디스크에 쓰지 않고 문서에 데이터 삽입

방법: 서버에 있는 통합 문서에서 캐시된 데이터 검색

방법: 서버에 있는 통합 문서에서 캐시된 데이터 변경

개념

서버에 있는 문서의 데이터 액세스

DiffGrams(ADO.NET)