Workbook.XmlImport 方法 (2007 system)

更新:2007 年 11 月

将 XML 数据文件导入当前工作簿。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel.v9.0(在 Microsoft.Office.Tools.Excel.v9.0.dll 中)

语法

声明
Public Function XmlImport ( _
    Url As String, _
    <OutAttribute> ByRef ImportMap As XmlMap, _
    Overwrite As Object, _
    Destination As Object _
) As XlXmlImportResult
用法
Dim instance As Workbook
Dim Url As String
Dim ImportMap As XmlMap
Dim Overwrite As Object
Dim Destination As Object
Dim returnValue As XlXmlImportResult

returnValue = instance.XmlImport(Url, _
    ImportMap, Overwrite, Destination)
public XlXmlImportResult XmlImport(
    string Url,
    out XmlMap ImportMap,
    Object Overwrite,
    Object Destination
)

参数

  • Url
    类型:System.String

    到 XML 数据文件的统一资源定位符 (URL) 或统一命名约定 (UNC) 路径。

  • Overwrite
    类型:System.Object

    如果未指定 Destination 参数的值,则此参数指定是否覆盖已映射到在 ImportMap 参数中指定的架构映射的数据。设置为 true 可覆盖数据,设置为 false 可向现有数据追加新数据。默认值为 true。如果未指定 Destination 参数的值,则此参数指定是否覆盖现有数据。设置为 true 可覆盖现有数据,设置为 false 可在要执行数据覆盖时取消导入。默认值为 true。

  • Destination
    类型:System.Object

    数据将导入指定的 Range 中的新 XML 列表。

返回值

类型:Microsoft.Office.Interop.Excel.XlXmlImportResult

XlXmlImportResult 值之一。

备注

如果要将数据导入现有映射,则不要指定 Destination 参数的值。

下面的情况会导致此方法生成运行时错误:

  • 指定的 XML 数据包含语法错误。

  • 由于指定的数据无法适合工作表而取消导入过程。

使用 XmlImportXml 方法导入先前加载到内存中的 XML 数据。

可选参数

有关可选参数的信息,请参见了解 Office 解决方案中的可选参数

示例

下面的代码示例根据从 DataSet 生成的架构创建一个 XmlMap,将 DataSet 写入一个 XML 文件,然后使用 XmlImport 方法将该 XML 文件中的数据写入工作表 Sheet1 中的范围。

此示例针对的是文档级自定义项。

Private Sub WorkbookXmlImport()

    ' 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 and write the XML to a file.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add(ds.GetXmlSchema(), _
        "NewDataSet")
    ds.WriteXml("C:\Customers.xml")

    ' Import the XML from the file.
    Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
    Me.XmlImport("C:\Customers.xml", xmlMap1, True, _
        range1)
End Sub
private void WorkbookXmlImport()
{
    // 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 and write the XML to a file.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(ds.GetXmlSchema(),
        "NewDataSet");
    ds.WriteXml(@"C:\Customers.xml");

    // Import the XML from the file.
    Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
    this.XmlImport(@"C:\Customers.xml", out xmlMap1, true, range1);
}

权限

另请参见

参考

Workbook 类

Workbook 成员

Microsoft.Office.Tools.Excel 命名空间