Evento Workbook.BeforeXmlImport (sistema de 2007)

Ocorre antes de uma conexão de dados XML é atualizada ou antes XML novo dados são importados para a pasta de trabalho.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (em Microsoft.Office.Tools.Excel.v9.0.dll)

Sintaxe

Public Event BeforeXmlImport As WorkbookEvents_BeforeXmlImportEventHandler

Dim instance As Workbook
Dim handler As WorkbookEvents_BeforeXmlImportEventHandler

AddHandler instance.BeforeXmlImport, handler
public event WorkbookEvents_BeforeXmlImportEventHandler BeforeXmlImport

Exemplos

O exemplo de código a seguir demonstra como importar dados XML para uma pasta de trabalho.O exemplo cria um DataSet do cliente nomeia e adiciona um XmlMap com base no esquema XML para o DataSet para o XmlMaps coleção da pasta de trabalho corrente. O exemplo, em seguida, chama o XmlImportXml método para importar os dados na planilha Sheet1. Quando o XmlImportXml método é chamado, o BeforeXmlImport manipulador de eventos solicita ao usuário para continuar ou cancelar a importação de XML e o AfterXmlImport manipulador de eventos relata se o XML foi importado com êxito.

Este exemplo é para uma personalização em nível de documento.

PrivateSub WorkbookXmlImportEvents()

    ' Create a new DataTable.Dim ds AsNew 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.IfNot (xmlMap1 IsNothing) Then
        ' This will raise the BeforeXmlImport and AfterXmlImport events.Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
        Me.XmlImportXml(ds.GetXml(), xmlMap1, True, _
            range1)
    Else
        MsgBox("The XmlMap could not be created")
    EndIfEndSubSub ThisWorkbook_BeforeXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal Url AsString, ByVal IsRefresh AsBoolean, _
    ByRef Cancel AsBoolean) HandlesMe.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 = TrueEndIfEndSubSub ThisWorkbook_AfterXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal IsRefresh AsBoolean, ByVal Result As Excel.XlXmlImportResult) _
    HandlesMe.AfterXmlImport

    If Result = Excel.XlXmlImportResult.xlXmlImportSuccess Then
        MsgBox("XML import succeeded.")
    Else
        MsgBox("XML import failed.")
    EndIfEndSub
privatevoid 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)
    {
        // This will raise the BeforeXmlImport and AfterXmlImport events.
        Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
        this.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, refbool 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.");
    }
}

Permissões

Consulte também

Referência

Workbook Classe

Membros Workbook

Namespace Microsoft.Office.Tools.Excel