How to generate XML from an Excel VBA macro?

前端 未结 4 1795
故里飘歌
故里飘歌 2021-01-02 23:14

So, I\'ve got a bunch of content that was delivered to us in the form of Excel spreadsheets. I need to take that content and push it into another system. The other system ta

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 23:52

    You might like to consider ADO - a worksheet or range can be used as a table.

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adPersistXML = 1
    
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    ''It wuld probably be better to use the proper name, but this is
    ''convenient for notes
    strFile = Workbooks(1).FullName
    
    ''Note HDR=Yes, so you can use the names in the first row of the set
    ''to refer to columns, note also that you will need a different connection
    ''string for >=2007
    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
            & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
    
    cn.Open strCon
    rs.Open "Select * from [Sheet1$]", cn, adOpenStatic, adLockOptimistic
    
    If Not rs.EOF Then
        rs.MoveFirst
        rs.Save "C:\Docs\Table1.xml", adPersistXML
    End If
    
    rs.Close
    cn.Close
    

提交回复
热议问题