WorkbookBase.BeforeXmlExport Event


Occurs before Microsoft Office Excel saves or exports data from the workbook to an XML data file.

Namespace:   Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

public event WorkbookEvents_BeforeXmlExportEventHandler BeforeXmlExport

This event will not occur when you are saving to the XML Spreadsheet file format.

The following code example demonstrates how to export XML data from a workbook. The example imports the XML data from a DataSet into a XmlMap in the current workbook. The example then calls the SaveAsXMLData method to export the data from the XmlMap into an XML file. When the SaveAsXMLData method is called, the BeforeXmlExport event handler reports that the XML is being exported, and the AfterXmlExport event handler reports whether the XML was successfully exported.

This example is for a document-level customization.

private void WorkbookXmlExportEvents()
    this.BeforeXmlExport +=
        new Excel.WorkbookEvents_BeforeXmlExportEventHandler(

    this.AfterXmlExport +=
        new Excel.WorkbookEvents_AfterXmlExportEventHandler(

    // 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";

    // Add a new XML map.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(

    // Import the data into Sheet1. 
    Excel.Range range1 = Globals.Sheet1.Range["A1"];
    this.XmlImportXml(ds.GetXml(), out xmlMap1, true,

    // Export the data.
    if (xmlMap1.IsExportable)
        this.SaveAsXMLData(this.Name + ".xml", xmlMap1);

void ThisWorkbook_BeforeXmlExport(Excel.XmlMap Map, string Url,
    ref bool Cancel)
    MessageBox.Show("Microsoft Excel is exporting XML from " +
        "the XmlMap.");

void ThisWorkbook_AfterXmlExport(Excel.XmlMap Map, string Url,
    Excel.XlXmlExportResult Result)
    if (Result == Excel.XlXmlExportResult.xlXmlExportSuccess)
        MessageBox.Show("XML export succeeded.");
        MessageBox.Show("XML export failed.");
Return to top