Export (0) Print
Expand All
Expand Minimize

Workbook.XmlMaps Property

Gets an Microsoft.Office.Interop.Excel.XmlMaps collection that represents the schema maps that have been added to the workbook.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual XmlMaps XmlMaps { get; }

Property Value

An Microsoft.Office.Interop.Excel.XmlMaps collection that represents the schema maps that have been added to the workbook.

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.

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 then reports whether the XML was successfully imported.

NoteNote

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 XmlImportXml topic.

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.");
    }
}

Show:
© 2015 Microsoft