Condividi tramite


WorkbookBase.XmlImportXml(String, XmlMap, Object, Object) Metodo

Definizione

Importa un flusso di dati XML precedentemente caricato in memoria.

public Microsoft.Office.Interop.Excel.XlXmlImportResult XmlImportXml (string data, out Microsoft.Office.Interop.Excel.XmlMap importMap, object overwrite, object destination);
member this.XmlImportXml : string * XmlMap * obj * obj -> Microsoft.Office.Interop.Excel.XlXmlImportResult
Public Function XmlImportXml (data As String, ByRef importMap As XmlMap, Optional overwrite As Object, Optional destination As Object) As XlXmlImportResult

Parametri

data
String

Dati da importare.

importMap
XmlMap

Mappa dello schema da applicare per l'importazione del file.

overwrite
Object

Se un valore non viene specificato per il Destination parametro, questo parametro specifica se sovrascrivere i dati mappati al mapping dello schema specificato nel ImportMap parametro. Impostare su true per sovrascrivere i dati o su false per aggiungere i nuovi dati a quelli esistenti. Il valore predefinito è true. Se viene specificato un valore per il Destination parametro, questo parametro specifica se sovrascrivere i dati esistenti. Impostare su true per sovrascrivere i dati esistenti o su false per annullare l'importazione se i dati vengono sovrascritti. Il valore predefinito è true.

destination
Object

I dati saranno importati in un nuovo elenco XML nell'oggetto Range specificato.

Restituisce

Uno dei valori di XlXmlImportResult.

Esempio

Nell'esempio di codice seguente viene illustrato come importare dati XML in una cartella di lavoro. L'esempio crea un di nomi dei clienti e aggiunge un DataSet oggetto XmlMap basato sullo schema XML per DataSet la XmlMaps raccolta della cartella di lavoro corrente. Nell'esempio viene quindi chiamato il XmlImportXml metodo per importare i dati nel foglio di lavoro Sheet1. Quando viene chiamato il XmlImportXml metodo, il BeforeXmlImport gestore eventi richiede all'utente di procedere o annullare l'importazione del codice XML e il AfterXmlImport gestore eventi segnala se il codice XML è stato importato correttamente.

Questo esempio è relativo a una personalizzazione a livello di documento.

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)
    {
        // This will raise the BeforeXmlImport and AfterXmlImport events.
        Excel.Range range1 = Globals.Sheet1.Range["A1"];
        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, 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.");
    }
}
Private Sub WorkbookXmlImportEvents()

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

    ' Import the data stream if the XmlMap was successfully created.
    If Not (xmlMap1 Is Nothing) 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")
    End If
End Sub

Sub ThisWorkbook_BeforeXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByVal IsRefresh As Boolean, _
    ByRef Cancel As Boolean) Handles Me.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 = True
    End If
End Sub

Sub ThisWorkbook_AfterXmlImport(ByVal Map As Excel.XmlMap, _
    ByVal IsRefresh As Boolean, ByVal Result As Excel.XlXmlImportResult) _
    Handles Me.AfterXmlImport

    If Result = Excel.XlXmlImportResult.xlXmlImportSuccess Then
        MsgBox("XML import succeeded.")
    Else
        MsgBox("XML import failed.")
    End If
End Sub

Commenti

Non specificare un valore per il Destination parametro se si desidera importare dati in un mapping esistente.

Le condizioni seguenti causeranno che questo metodo generi errori di runtime:

  • I dati XML specificati contengono errori di sintassi.

  • Il processo di importazione è stato annullato perché i dati specificati non possono essere inseriti nel foglio di lavoro.

Usare il XmlImport metodo per importare un file di dati XML nella cartella di lavoro corrente.

Parametri facoltativi

Per informazioni sui parametri facoltativi, vedere parametri facoltativi nelle soluzioni Office.

Si applica a