共用方式為


ServerDocument 建構函式 (String) (2003 系統)

使用要載入之文件的路徑,初始化 ServerDocument 類別的新執行個體。

命名空間:  Microsoft.VisualStudio.Tools.Applications.Runtime
組件:  Microsoft.VisualStudio.Tools.Applications.Runtime (在 Microsoft.VisualStudio.Tools.Applications.Runtime.dll 中)

語法

'宣告
Public Sub New ( _
    documentPath As String _
)
'用途
Dim documentPath As String

Dim instance As New ServerDocument(documentPath)
public ServerDocument(
    string documentPath
)

參數

例外狀況

例外狀況 條件
ArgumentNullException

documentPath 參數為 null Nothing nullptr Null 參照 (即 Visual Basic 中的 Nothing) ,或空字串,或整個都是空白字元。

FileNotFoundException

documentPath 所指定的檔案不存在。

IOException

documentPath 所指定的檔案為唯讀或無法存取。

CannotLoadManifestException

documentPath 所指定的檔案不代表含有 Visual Studio Tools for Office 自訂的文件。

備註

您可以使用這個建構函式,存取磁碟上文件中的快取資料或應用程式資訊清單。當您使用這個建構函式時,指定的文件會以讀取/寫入存取權開啟。

若要使用這個建構函式,文件必須已經有 Visual Studio Tools for Office 自訂。如果您想要開啟尚未有 Visual Studio Tools for Office 自訂的文件,請使用 ServerDocument..::.ServerDocument(String, Boolean)ServerDocument..::.ServerDocument(String, Boolean, FileAccess) 建構函式,並將 onClient 參數設定為 true。用戶端電腦必須安裝 Word 或 Excel,才能使用這些建構函式。

範例

下列程式碼範例會使用 ServerDocument(String) 建構函式,建立新的 ServerDocument,載入指定的文件然後對文件的快取資料進行變更。這個範例需要對 Microsoft.VisualStudio.Tools.Applications.Runtime 組件的參考,並且在程式碼檔的開頭需要 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空間的 Imports (適用於 Visual Basic) 或 using (適用於 C#) 陳述式。

Private customerOrdersSet As DataSet

Private Sub LoadCachedData(ByVal fileName As String)
    If ServerDocument.IsCacheEnabled(fileName) Then
        Dim serverDocument1 As ServerDocument = Nothing
        Try
            serverDocument1 = New ServerDocument(fileName)
            customerOrdersSet = New DataSet()

            ' Identify the namespace and the class that  
            ' contains the cached data object.
            Dim hostItem1 As CachedDataHostItem = _
                serverDocument1.CachedData.HostItems( _
                "DataCachingSource.ThisWorkbook")

            ' Identify the name of the cached data object.
            Dim dataItem1 As CachedDataItem = _
                hostItem1.CachedData("customerOrdersCached")

            ' Read the cached data into the local DataSet.
            If Nothing <> dataItem1.Xml AndAlso _
                Nothing <> dataItem1.Schema Then

                Dim xmlReader As New System.IO.StringReader( _
                    dataItem1.Xml)
                Dim schemaReader As New System.IO.StringReader( _
                    dataItem1.Schema)
                customerOrdersSet.ReadXmlSchema(schemaReader)
                customerOrdersSet.ReadXml(xmlReader)

                ' Modify the data.
                Dim row As DataRow
                For Each row In customerOrdersSet.Tables(0).Rows
                    Dim orders As Integer = Fix(row("OrderQuantity"))
                    row("OrderQuantity") = orders * 2
                Next row

                ' Write the modified data back to the
                ' cached data in the worksheet.
                dataItem1.SerializeDataInstance(customerOrdersSet)
                serverDocument1.Save()
            End If
        Finally
            If Not serverDocument1 Is Nothing Then
                serverDocument1.Close()
            End If
        End Try
    Else
        MsgBox("The specified document does not have " + _
            "a data cache.")
    End If
End Sub
private DataSet customerOrdersSet;

private void LoadCachedData(string fileName)
{
    if (ServerDocument.IsCacheEnabled(fileName))
    {
        ServerDocument serverDocument1 = null;
        try
        {
            serverDocument1 = new ServerDocument(fileName);
            customerOrdersSet = new DataSet();

            // Identify the namespace and the class that contains 
            // the cached data object.
            CachedDataHostItem hostItem1 =
                serverDocument1.CachedData.HostItems[
                "DataCachingSource.ThisWorkbook"];

            // Identify the name of the cached data object.
            CachedDataItem dataItem1 = hostItem1.CachedData[
                "customerOrdersCached"];

            // Read the cached data into the local DataSet.
            if (null != dataItem1.Xml && null != dataItem1.Schema)
            {
                System.IO.StringReader xmlReader =
                    new System.IO.StringReader(dataItem1.Xml);
                System.IO.StringReader schemaReader =
                    new System.IO.StringReader(dataItem1.Schema);
                customerOrdersSet.ReadXmlSchema(schemaReader);
                customerOrdersSet.ReadXml(xmlReader);

                // Modify the data.
                foreach (DataRow row in
                    customerOrdersSet.Tables[0].Rows)
                {
                    int orders = (int)row["OrderQuantity"];
                    row["OrderQuantity"] = orders * 2;
                }

                // Write the modified data back to the
                // cached data in the worksheet.
                dataItem1.SerializeDataInstance(customerOrdersSet);
                serverDocument1.Save();
            }
        }
        finally
        {
            if (serverDocument1 != null)
                serverDocument1.Close();
        }
    }
    else
    {
        MessageBox.Show("The specified document does not have " +
            "a data cache.");
    }
}

使用權限

請參閱

參考

ServerDocument 類別

ServerDocument 成員

ServerDocument 多載

Microsoft.VisualStudio.Tools.Applications.Runtime 命名空間