Condividi tramite


Evento WorkbookBase.BeforeXmlExport

Si verifica prima del salvataggio o dell'esportazione di dati dalla cartella di lavoro in un file di dati XML.

Spazio dei nomi:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Sintassi

'Dichiarazione
Public Event BeforeXmlExport As WorkbookEvents_BeforeXmlExportEventHandler
public event WorkbookEvents_BeforeXmlExportEventHandler BeforeXmlExport

Note

Questo evento non si verifica se si salva nel formato di file Foglio di calcolo XML.

Esempi

Nell'esempio di codice riportato di seguito viene illustrato come esportare dati XML da una cartella di lavoro. In questo esempio i dati XML vengono importati da un oggetto DataSet in un oggetto XmlMap nella cartella di lavoro corrente. Viene quindi chiamato il metodo SaveAsXMLData per esportare i dati da XmlMap in un file XML. Quando il metodo SaveAsXMLData viene chiamato, il gestore eventi BeforeXmlExport segnala che l'esportazione dei dati XML è in corso e il gestore eventi AfterXmlExport indica se l'esportazione dei dati XML si è conclusa correttamente.

Questo esempio è valido per una personalizzazione a livello di documento.

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
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(), missing);

    // Import the data into Sheet1. 
    Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
    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.");
    }
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

WorkbookBase Classe

Spazio dei nomi Microsoft.Office.Tools.Excel