WorkbookBase.XmlImport 方法

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

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

語法

'宣告
Public Function XmlImport ( _
    url As String, _
    <OutAttribute> ByRef importMap As XmlMap, _
    overwrite As Object, _
    destination As Object _
) As XlXmlImportResult
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。

傳回值

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

.NET Framework 安全性

請參閱

參考

WorkbookBase 類別

Microsoft.Office.Tools.Excel 命名空間