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并根据 的 XML 架构DataSet将 添加到XmlMapXmlMaps当前工作簿的集合中。 然后,该示例调用 XmlImportXml 方法将数据导入工作表 Sheet1XmlImportXml调用 方法时,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 解决方案中的可选参数

适用于