Save as XML with VBA?

后端 未结 2 1602
执笔经年
执笔经年 2021-01-11 22:55

Is there any way to save an Excel table as XML? I have the XML Schema file... and some data in a table... and I have in Excel the Save as XML file option but can I save a fi

2条回答
  •  再見小時候
    2021-01-11 23:24

    This link helped me the most -> http://curiousmind.jlion.com/exceltotextfile

    Script on link:

    Sub MakeXML(iCaptionRow As Integer, iDataStartRow As Integer, sOutputFileName As String)
        Dim Q As String
        Q = Chr$(34)
    
        Dim sXML As String
    
        sXML = ""
        sXML = sXML & ""
    
    
        ''--determine count of columns
        Dim iColCount As Integer
        iColCount = 1
        While Trim$(Cells(iCaptionRow, iColCount)) > ""
            iColCount = iColCount + 1
        Wend
    
        Dim iRow As Integer
        iRow = iDataStartRow
    
        While Cells(iRow, 1) > ""
            sXML = sXML & ""
    
            For icol = 1 To iColCount - 1
               sXML = sXML & "<" & Trim$(Cells(iCaptionRow, icol)) & ">"
               sXML = sXML & Trim$(Cells(iRow, icol))
               sXML = sXML & ""
            Next
    
            sXML = sXML & ""
            iRow = iRow + 1
        Wend
        sXML = sXML & ""
    
        Dim nDestFile As Integer, sText As String
    
        ''Close any open text files
        Close
    
        ''Get the number of the next free text file
        nDestFile = FreeFile
    
        ''Write the entire file to sText
        Open sOutputFileName For Output As #nDestFile
        Print #nDestFile, sXML
        Close
    End Sub
    
    Sub test()
        MakeXML 1, 2, "C:\Users\jlynds\output2.xml"
    End Sub
    

提交回复
热议问题