XML

时间:2016-10-10 13:10:55

标签: xml vba

我找到了一个使用表头将选定范围(表)导出为xml格式的代码。但我想改编这段代码。

每次我想用1增加“Row_number”时,请参阅image。请参阅下面的VBA代码。有人可以帮我调整这段代码吗?代码来自此website

Public Sub ExportRangeToXML()

Dim strXML As String
Dim varTable As Variant
Dim intRow As Integer
Dim intCol As Integer
Dim intFileNum As Integer
Dim strFilePath As String
Dim strRowElementName As String
Dim strTableElementName As String
Dim varColumnHeaders As Variant

    'Set custom names
    strTableElementName = "Table"
    strRowElementName = "Row_number"

    'Set file path
    strFilePath = Application.GetSaveAsFilename(, "(*.xml),*.xml", , "Save As...")
    If strFilePath = vbNullString Then Exit Sub

    'Get table data
    varTable = Selection.Value
    varColumnHeaders = Selection.Rows(1).Value

    'Build xml
    strXML = "<?xml version=""1.0"" encoding=""utf-8""?>"
    strXML = strXML & "<" & strTableElementName & ">"
    For intRow = 2 To UBound(varTable, 1)
        strXML = strXML & "<" & strRowElementName & ">"
        For intCol = 1 To UBound(varTable, 2)
            strXML = strXML & "<" & varColumnHeaders(1, intCol) & ">" & _
                varTable(intRow, intCol) & "</" & varColumnHeaders(1, intCol) & ">"
        Next
        strXML = strXML & "</" & strRowElementName & ">"
    Next
    strXML = strXML & "</" & strTableElementName & ">"

    'Get next file number
    intFileNum = FreeFile

    'Open the file, write output, then close file
    Open strFilePath For Output As #intFileNum
    Print #intFileNum, strXML
    Close #intFileNum

End Sub

0 个答案:

没有答案