简体   繁体   English

从excel表中读取数据并使用vba宏将其输出到xml?

[英]Reading data from excel table and outputting it to xml using vba macro?

I have some data in an excel table in a format similar to the one below. 我在excel表中有一些数据,格式类似于下面的格式。

    Colum1_Heading       Column2_Heading
          a                   1
          b                   2
          c                   3

I am trying to convert the data in this table to XML, I will always have the same number of columns but an indeterminate number of rows. 我试图将此表中的数据转换为XML,我将始终具有相同数量的列但行数不确定。

The XML format is something like this XML格式是这样的

<?xml version="1.0" encoding="utf-8"?>
<root> 
    <tag1>
         <tag2>
              a - 1,
              b - 2,
              c - 3
         </tag2>
    </tag1>
</root>

So I think it should be fairly simple. 所以我认为应该相当简单。 So far I've started writing some code that creates a writeable string which I would then write to a new XML file, but I'm pretty new to VBA so I'm still researching a bunch on the internet. 到目前为止,我已经开始编写一些代码来创建一个可写的字符串,然后我将其写入一个新的XML文件,但我对VBA很新,所以我还在网上研究一堆。 I've inputted the header and the beginning and end tags since those will always be the same. 我输入了标题以及开头和结尾标记,因为它们总是一样的。 My question is how to read the rows of the table and write them to the XML in a format shown above. 我的问题是如何读取表的行并以上面显示的格式将它们写入XML。 Any help would be appreciated. 任何帮助,将不胜感激。 Please let me know if you need any additional info. 如果您需要任何其他信息,请与我们联系。

This should get you started. 这应该让你开始。 Add a reference to MSXML2: 添加对MSXML2的引用:

Sub Test()
    With New MSXML2.DOMDocument60

        Dim root As IXMLDOMNode
        Set root = .createElement("root")

        Dim tag1 As IXMLDOMNode
        Set tag1 = .createElement("tag1")

        Dim tag2 As IXMLDOMNode
        Set tag2 = .createElement("tag2")

        tag2.Text = ReadXmlContentFromWorksheet '"a - 1,b - 2,c - 3"

        tag1.appendChild tag2
        root.appendChild tag1

        .appendChild .createProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8""")
        Set .DocumentElement = root
        '.Save "test.xml"
        Debug.Print .XML

    End With
End Sub

Output: 输出:

 <?xml version="1.0"?> <root><tag1><tag2>a - 1,b - 2,c - 3</tag2></tag1></root> 

Note that the vertical whitespace is irrelevant 请注意,垂直空白是无关紧要的


The ReadXmlContentFromWorksheet function would look something like this: ReadXmlContentFromWorksheet函数看起来像这样:

Private Function ReadXmlContentFromWorksheet() As String
    Dim result As String

    Dim lastRow As Long
    lastRow = MyDataSheet.Range("A" & MyDataSheet.Rows.Count).End(xlUp).Row

    Dim currentRow As Long
    For currentRow = 1 To lastRow
        result = result & MyDataSheet.Cells(currentRow, 1).Value & " - " _
                        & MyDataSheet.Cells(currentRow, 2).Value
        result = IIf(currentRow = lastRow, vbNullString, ",")
    Next

    ReadXmlContentFromWorksheet = result
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM