WorkbookBase.XmlImportXml 方法

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

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
Public Function XmlImportXml ( _
    data As String, _
    <OutAttribute> ByRef importMap As XmlMap, _
    overwrite As Object, _
    destination As Object _
) As XlXmlImportResult
public XlXmlImportResult XmlImportXml(
    string data,
    out XmlMap importMap,
    Object overwrite,
    Object destination
)

參數

  • overwrite
    型別:System.Object
    如果未指定 Destination 參數的值,則此參數指出是否覆寫已對應至 ImportMap 參數指定的結構描述對應之資料。設定為 true 表示要覆寫資料,設定為 false 表示要將新資料附加至現有資料中。預設值是 true。如果已指定 Destination 參數的值,此參數會指出是否覆寫現有資料。設定為 true 表示要覆寫現有資料,如果資料可能會被覆寫,則設定為 false 以取消匯入。預設值是 true。

傳回值

型別:Microsoft.Office.Interop.Excel.XlXmlImportResult
其中一個 XlXmlImportResult 值。

備註

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

下列情況會導致這個方法產生執行階段錯誤:

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

  • 取消匯入程序,因為指定的資料在工作表中無法適用。

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

選擇性參數

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

範例

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

這是示範文件層級自訂的範例。

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
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.");
    }
}

.NET Framework 安全性

請參閱

參考

WorkbookBase 類別

Microsoft.Office.Tools.Excel 命名空間