Compartilhar via


WorkbookBase.BeforeXmlExport Event

Definition

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

public:
 event Microsoft::Office::Interop::Excel::WorkbookEvents_BeforeXmlExportEventHandler ^ BeforeXmlExport;
public event Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeXmlExportEventHandler BeforeXmlExport;
member this.BeforeXmlExport : Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeXmlExportEventHandler 
Public Custom Event BeforeXmlExport As WorkbookEvents_BeforeXmlExportEventHandler 

Event Type

Examples

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(
        ThisWorkbook_BeforeXmlExport);

    this.AfterXmlExport +=
        new Excel.WorkbookEvents_AfterXmlExportEventHandler(
        ThisWorkbook_AfterXmlExport);

    // 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.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(
        ds.GetXmlSchema());

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

    // 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.");
    }
    else
    {
        MessageBox.Show("XML export failed.");
    }
}
Private Sub WorkbookXmlExportEvents()

    ' 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.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add( _
        ds.GetXmlSchema())

    ' Import the data into Sheet1. 
    Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
    Me.XmlImportXml(ds.GetXml(), xmlMap1, True, _
        range1)

    If xmlMap1.IsExportable Then
        Me.SaveAsXMLData(Me.Name & ".xml", xmlMap1)
    End If
End Sub

Sub ThisWorkbook_BeforeXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByRef Cancel As Boolean) _
    Handles Me.BeforeXmlExport

    MsgBox("Microsoft Excel is exporting XML from " & _
        "the XmlMap.")
End Sub

Sub ThisWorkbook_AfterXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByVal Result As Excel.XlXmlExportResult) _
    Handles Me.AfterXmlExport

    If Result = Excel.XlXmlExportResult.xlXmlExportSuccess Then
        MsgBox("XML export succeeded.")
    Else
        MsgBox("XML export failed.")
    End If
End Sub

Remarks

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

Applies to