开发者

How to transform an excel table in a xml file?

I have an Excel file with three worksheets. One of these sheets is a table with 3500 lines, each with 200 columns. I need to turn this table into an xml file with the following form:

<xml>
    <element columm1="attribute1" columm2="attribute2" />
</xml>

Where "columnN" is the name of each column and "attributeN开发者_JAVA百科" is the value of it for each line. Each "element" represents a table row.

I do not know what would be the simplest way to do this. I believe this could be done using macros or VBA, but I'm not sure because I do not know the language well.

Ideally, the process could be adapted if the table format change (with more rows or columns) or excel file changes (including more spreadsheets, for example). Many thanks for the help.


This should do:

Sub ToXML()


MyFile = "data.xml"
fnum = FreeFile()
Open MyFile For Output As fnum

Dim n As Integer
n = 3 'Number of DATA rows

Dim m As Integer
m = 2 ' Number of columns

Print #fnum, "<xml>"

For i = 2 To n + 1
    Print #fnum, "<element ";
    For j = 1 To m
        Print #fnum, Cells(1, j).Value & "=""" & Cells(i, j).Value & """ ";
    Next j
Print #fnum, "/>"
Next i

Print #fnum, "</xml>"
Close #fnum
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜