WorkbookBase.XmlImportXml(String, XmlMap, Object, Object) 方法

定義

匯入先前已載入記憶體的 XML 資料流。

public Microsoft.Office.Interop.Excel.XlXmlImportResult XmlImportXml (string data, out Microsoft.Office.Interop.Excel.XmlMap importMap, object overwrite, object destination);
member this.XmlImportXml : string * XmlMap * obj * obj -> Microsoft.Office.Interop.Excel.XlXmlImportResult
Public Function XmlImportXml (data As String, ByRef importMap As XmlMap, Optional overwrite As Object, Optional destination As Object) As XlXmlImportResult

參數

data
String

要匯入的資料。

importMap
XmlMap

匯入檔案時套用的結構描述對應。

overwrite
Object

如果未為 Destination 參數指定值,則此參數會指定是否覆寫已對應至 參數中所 ImportMap 指定架構對應的數據。 設定為 true 表示要覆寫資料,設定為 false 表示要將新資料附加至現有資料中。 預設值是 true。 如果為 Destination 參數指定值,則此參數會指定是否要覆寫現有的數據。 設定為 true 表示要覆寫現有資料,如果資料可能會被覆寫,則設定為 false 以取消匯入。 預設值是 true

destination
Object

資料會匯入 Range 指定的新 XML 清單。

傳回

其中一個 XlXmlImportResult 值。

範例

下列程式代碼範例示範如何將 XML 資料匯入活頁簿。 此範例會建立客戶名稱的 , DataSet 並根據 XmlMap 的 XML 架構 DataSet ,將 加入至 XmlMaps 目前活頁簿的集合。 然後,此範例會 XmlImportXml 呼叫 方法,將數據匯入工作表 Sheet1中。 XmlImportXml呼叫 方法時,BeforeXmlImport事件處理程式會提示用戶繼續或取消匯入 XML,而事件處理程式會AfterXmlImport報告 XML 是否已成功匯入。

此範例適用於檔層級自定義。

private void WorkbookXmlImportEvents()
{
    this.BeforeXmlImport +=
        new Excel.WorkbookEvents_BeforeXmlImportEventHandler(
        ThisWorkbook_BeforeXmlImport);

    this.AfterXmlImport += new
        Excel.WorkbookEvents_AfterXmlImportEventHandler(
        ThisWorkbook_AfterXmlImport);

    // Create a new DataTable.
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Customers");
    dt.Columns.Add(new DataColumn("LastName"));
    dt.Columns.Add(new DataColumn("FirstName"));

    // Add a new row to the DataTable.
    DataRow dr = dt.NewRow();
    dr["LastName"] = "Chan";
    dr["FirstName"] = "Gareth";
    dt.Rows.Add(dr);

    // Add a new XML map to the collection.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(ds.GetXmlSchema(),
        "NewDataSet");

    // Import the data stream if the XmlMap was successfully created.
    if (xmlMap1 != null)
    {
        // This will raise the BeforeXmlImport and AfterXmlImport events.
        Excel.Range range1 = Globals.Sheet1.Range["A1"];
        this.XmlImportXml(ds.GetXml(), out xmlMap1, true,
            range1);
    }
    else
    {
        MessageBox.Show("The XmlMap could not be created");
    }
}

void ThisWorkbook_BeforeXmlImport(Excel.XmlMap Map,
    string Url, bool IsRefresh, ref bool Cancel)
{
    if (DialogResult.No == MessageBox.Show("Microsoft Excel is about" +
        " to import XML into the workbook. Continue with importing?",
        "Custom XML Import Dialog", MessageBoxButtons.YesNo))
    {
        Cancel = true;
    }
}

void ThisWorkbook_AfterXmlImport(Excel.XmlMap Map, bool IsRefresh,
    Excel.XlXmlImportResult Result)
{
    if (Result == Excel.XlXmlImportResult.xlXmlImportSuccess)
    {
        MessageBox.Show("XML import succeeded.");
    }
    else
    {
        MessageBox.Show("XML import failed.");
    }
}
Private Sub WorkbookXmlImportEvents()

    ' Create a new DataTable.
    Dim ds As New DataSet()
    Dim dt As DataTable = ds.Tables.Add("Customers")
    dt.Columns.Add(New DataColumn("LastName"))
    dt.Columns.Add(New DataColumn("FirstName"))

    ' Add a new row to the DataTable.
    Dim dr As DataRow = dt.NewRow()
    dr("LastName") = "Chan"
    dr("FirstName") = "Gareth"
    dt.Rows.Add(dr)

    ' Add a new XML map to the collection.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add(ds.GetXmlSchema(), _
        "NewDataSet")

    ' Import the data stream if the XmlMap was successfully created.
    If Not (xmlMap1 Is Nothing) Then

        ' This will raise the BeforeXmlImport and AfterXmlImport events.
        Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
        Me.XmlImportXml(ds.GetXml(), xmlMap1, True, _
            range1)
    Else
        MsgBox("The XmlMap could not be created")
    End If
End Sub

Sub ThisWorkbook_BeforeXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByVal IsRefresh As Boolean, _
    ByRef Cancel As Boolean) Handles Me.BeforeXmlImport

    If DialogResult.No = MessageBox.Show("Microsoft Excel is about" & _
        " to import XML into the workbook. Continue with importing?", _
        "Custom XML Import Dialog", MessageBoxButtons.YesNo) Then
        Cancel = True
    End If
End Sub

Sub ThisWorkbook_AfterXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal IsRefresh As Boolean, ByVal Result As Excel.XlXmlImportResult) _
    Handles Me.AfterXmlImport

    If Result = Excel.XlXmlImportResult.xlXmlImportSuccess Then
        MsgBox("XML import succeeded.")
    Else
        MsgBox("XML import failed.")
    End If
End Sub

備註

如果您想要將數據匯入至現有的對應, Destination 請勿指定 參數的值。

下列條件會導致此方法產生執行時錯誤:

  • 指定的 XML 資料包含語法錯誤。

  • 匯入程式已取消,因為指定的數據無法放入工作表中。

使用 方法, XmlImport 將 XML 資料文件匯入目前的活頁簿。

選擇性參數

如需選擇性參數的資訊,請參閱 Office 方案中的選擇性參數

適用於