Excel vba xml parsing performance

前端 未结 2 782
别那么骄傲
别那么骄傲 2021-01-27 04:24

I\'m working on taking some input data in excel, parsing it to xml and using that to run a SQL stored procedure, but I\'m running into performance issue on the xml parsing. The

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-27 05:06

    Consider using MSXML, a comprehensive W3C compliant library of XML APIs which you can use to build your XML with DOM methods (createElement, appendChild, setAttribute) instead of concatenating text strings. XML is not quite a text file but a markup file with encoding and tree structure. Excel comes equipped with the MSXML COM object by reference or late-binding, and can iteratively build a tree from Excel data as shown below.

    With 300 rows by 12 cols of random dates, below didn't even take a minute (literally seconds after clicking macro) AND it even pretty printed raw output with line breaks and indentation using an embedded XSLT stylesheet (if you do not pretty print, the MSXML outputs document as one long, continuous line).

    Input

    VBA (of course align to actual data)

    Sub xmlExport()
    On Error GoTo ErrHandle
        ' VBA REFERENCE MSXML, v6.0 '
        Dim doc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60, newDoc As New MSXML2.DOMDocument60
        Dim root As IXMLDOMElement, dataNode As IXMLDOMElement, datesNode As IXMLDOMElement, namesNode As IXMLDOMElement
        Dim i As Long, j As Long
        Dim tmpValue As Variant
    
        ' DECLARE XML DOC OBJECT '
        Set root = doc.createElement("DataSet")
        doc.appendChild root
    
        ' ITERATE THROUGH ROWS '
        For i = 2 To Sheets(1).UsedRange.Rows.Count
    
            ' DATA ROW NODE '
            Set dataNode = doc.createElement("DataRow")
            root.appendChild dataNode
    
            ' DATES NODE '
            Set datesNode = doc.createElement("Dates")
            datesNode.Text = Sheets(1).Range("A" & i)
            dataNode.appendChild datesNode
    
            ' NAMES NODE '
            For j = 1 To 12
                tmpValue = Sheets(1).Cells(i, j + 1)
                If IsDate(tmpValue) And Not IsNumeric(tmpValue) Then
                    Set namesNode = doc.createElement("Name" & j)
                    namesNode.Text = Format(tmpValue, "yyyy-mm-dd")
                    dataNode.appendChild namesNode
                End If
            Next j
    
        Next i
    
        ' PRETTY PRINT RAW OUTPUT '
        xslDoc.LoadXML "" _
                & "" _
                & "" _
                & "" _
                & " " _
                & "  " _
                & "   " _
                & "  " _
                & " " _
                & ""
    
        xslDoc.async = False
        doc.transformNodeToObject xslDoc, newDoc
        newDoc.Save ActiveWorkbook.Path & "\Output.xml"
    
        MsgBox "Successfully exported Excel data to XML!", vbInformation
        Exit Sub
    
    ErrHandle:
        MsgBox Err.Number & " - " & Err.Description, vbCritical
        Exit Sub
    
    End Sub
    

    Output

    
    
        
            Date1
            2016-04-23
            2016-09-22
            2016-09-23
            2016-09-24
            2016-10-31
            2016-09-26
            2016-09-27
            2016-09-28
            2016-09-29
            2016-09-30
            2016-10-01
            2016-10-02
        
        
            Date2
            2016-06-27
            2016-08-14
            2016-07-08
            2016-08-22
            2016-11-03
            2016-07-28
            2016-08-23
            2016-11-01
            2016-11-01
            2016-08-11
            2016-08-18
            2016-09-23
        
        ...
    

提交回复
热议问题