Imports an XML data stream that has been previously loaded into memory.
Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)
Visual Basic (Declaration)
Public Overridable Function XmlImportXml ( _
Data As String, _
<OutAttribute> ByRef ImportMap As XmlMap, _
<OptionalAttribute> Overwrite As Object, _
<OptionalAttribute> Destination As Object _
) As XlXmlImportResult
public virtual XlXmlImportResult XmlImportXml (
string Data,
out XmlMap ImportMap,
[OptionalAttribute] Object Overwrite,
[OptionalAttribute] Object Destination
)
Parameters
- Data
The data to import.
- ImportMap
The schema map to apply when importing the file.
- Overwrite
If a value is not specified for the Destination parameter, then this parameter specifies whether or not to overwrite data that has been mapped to the schema map specified in the ImportMap parameter. Set to true to overwrite the data or false to append the new data to the existing data. The default value is true. If a value is specified for the Destination parameter, then this parameter specifies whether or not to overwrite existing data. Set to true to overwrite existing data or false to cancel the import if data would be overwritten. The default value is true.
- Destination
The data will be imported into a new XML list in the Range specified.
Return Value
One of the XlXmlImportResult values.
XML features, except for saving files in the XML Spreadsheet format, are available only in Microsoft Office Professional Edition 2003 and Microsoft Office Excel 2003.
Do not specify a value for the Destination parameter if you want to import data into an existing mapping.
The following conditions will cause this method to generate run-time errors:
Use the XmlImport method to import an XML data file into the current workbook.
Usage Requirements
XmlImportXml does not work when the ExcelLocale1033Attribute attribute in your project is set to true (the default). If the ExcelLocale1033Attribute attribute is true, you must perform the following steps:
-
You must call the XmlImportXml method of the underlying Microsoft.Office.Interop.Excel.Workbook, instead of the Microsoft.Office.Tools.Excel.Workbook host item.
-
You must use the Unwrap method on all of the following objects before calling XmlImportXml.
-
The underlying Microsoft.Office.Interop.Excel.Workbook.
-
The XmlMap that is passed into the ImportMap parameter.
-
The Range that is passed into the Destination parameter.
Unwrap modifies these objects so that they pass the locale ID of the current thread to the Excel object model, instead of passing locale ID 1033.
The code example in this topic shows how to use XmlImportXml when the ExcelLocale1033Attribute attribute is true. For more information about ExcelLocale1033Attribute and the Unwrap method, see Formatting Data in Excel with Various Regional Settings.
Optional Parameters
The following code example demonstrates how to import XML data into a workbook. The example creates a DataSet of customer names and adds an XmlMap based on the XML schema for the DataSet to the XmlMaps collection of the current workbook. The example then calls the XmlImportXml method to import the data into worksheet Sheet1. When the XmlImportXml method is called, the BeforeXmlImport event handler prompts the user to either proceed with or cancel importing the XML, and the AfterXmlImport event handler reports whether the XML was successfully imported.
Note |
|---|
| This example assumes that the ExcelLocale1033Attribute attribute is true (the default). If the ExcelLocale1033Attribute attribute is false, you can call the XmlImportXml method of the Microsoft.Office.Tools.Excel.Workbook host item, and you do not need to use the Unwrap method. For more information, see the Remarks section in this topic. |
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
' Get objects that can be used while the ExcelLocale1033
' attribute is true (the default).
Dim workbook1 As Excel.Workbook = TryCast(Microsoft.Office.Tools. _
Excel.ExcelLocale1033Proxy.Unwrap(Me.InnerObject), Excel.Workbook)
Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
range1 = TryCast(Microsoft.Office.Tools.Excel. _
ExcelLocale1033Proxy.Unwrap(range1), Excel.Range)
xmlMap1 = TryCast(Microsoft.Office.Tools.Excel. _
ExcelLocale1033Proxy.Unwrap(xmlMap1), Excel.XmlMap)
' This will raise the BeforeXmlImport and AfterXmlImport events.
workbook1.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)
{
// Get objects that can be used while the ExcelLocale1033
// attribute is true (the default).
Excel.Workbook workbook1 = (Excel.Workbook)Microsoft.Office.Tools.
Excel.ExcelLocale1033Proxy.Unwrap(this.InnerObject);
Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
range1 = (Excel.Range)Microsoft.Office.Tools.Excel.
ExcelLocale1033Proxy.Unwrap(range1);
xmlMap1 = (Excel.XmlMap)Microsoft.Office.Tools.Excel.
ExcelLocale1033Proxy.Unwrap(xmlMap1);
// This will raise the BeforeXmlImport and AfterXmlImport events.
workbook1.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.");
}
}