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

定義

將 XML 資料檔匯入目前活頁簿。

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

參數

url
String

連接 XML 資料檔的統一資源定位器 (URL) 或統一命名慣例 (UNC) 路徑。

importMap
XmlMap

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

overwrite
Object

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

destination
Object

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

傳回

其中一個 XlXmlImportResult 值。

範例

下列程式代碼範例會根據 產生的架構建立 XmlMap ,並將 寫入 DataSet XML 檔案,然後使用 XmlImport 方法將 XML 檔案中的數據寫入工作表Sheet1中的DataSet範圍。

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

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"];
    this.XmlImport(@"C:\Customers.xml", out xmlMap1, true, range1);
}
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

備註

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

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

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

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

使用方法匯 XmlImportXml 入先前已載入記憶體的 XML 數據。

選擇性參數

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

適用於