转载:How to create XML file from scratch? - Simple Excel VBA
This is the third and the last part from the XML trilogy. Be sure You didn’t miss how to convert XML into Excel and how to overwrite XML file without copying or converting. In this article I’m going to show You how to create XML file from scratch.
So let’s call the whole worksheet as catalog filled with products. Products are stored only in the first column, features are in the rest of the columns.
So, the child nodes of the catalog (worksheet) are products (1st column). Features are child nodes of the products. Summing up – 3 levels. The first one is known, there is only 1 catalog, which opens in the beginning and closes in the end. Second is products and the third one is features.
We can see how many products and features are in the table, but let’s write the code universal, in case of change.
To start coding in the first place You need your future XML file path.
1
xml_file = "...\test2.xml"
Then create the file and mark it as output with number.
1
Open xml_file For Output As #1
Now every time You want to write a new line in the xml_file just put Print method and type the number You chose above with hashtag.
Let’s start manually with typing CATALOG.
1
Print #1, "<CATALOG>"
Now the hardest part. You need 2 loops – for columns and rows. While looping in columns remember, that first column is the name of the product – start with Product tag, in the second the features begin and after the last feature (last column) You need to end with Product tag. Just like in the XML structure I showed You up there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
For i = 2To lastRow For j = 1To lastCol head = UCase(.Cells(1, j)) val = .Cells(i, j) If j = 1Then prod = head Print #1, "<" & prod & " name=""" & val & """>" Else Print #1, "<" & head & ">" & val & "</" & head & ">" If j = lastCol Then Print #1, "</" & prod & ">" EndIf EndIf Next Next
In the last part, after all loops, You need to close the CATALOG and close the output xml_file.
Dim xml_file AsString, head AsString, val AsString Dim prod AsString Dim lastRow AsLong, lastCol AsLong, i AsLong, j AsLong Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1) xml_file = "...\test2.xml"
Open xml_file For Output As #1 Print #1, "<CATALOG>"
With ws lastRow = .Cells(Rows.Count, 1).End(xlUp).Row lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2To lastRow For j = 1To lastCol head = UCase(.Cells(1, j)) val = .Cells(i, j) If j = 1Then prod = head Print #1, "<" & prod & " name=""" & val & """>" Else Print #1, "<" & head & ">" & val _ & "</" & head & ">" If j = lastCol Then Print #1, "</" & prod & ">" EndIf EndIf Next Next
And this is it! End of the last part from this XML trilogy! Now You should know how to create XML file from scratch along with converting XML into Excel and overwriting XML. I hope You enjoyed that and those articles will be helpful at least a little bit.
Sub CreateXML() Dim xml_filepath AsString, tag AsString, val AsString, indent_space AsLong, need_trim AsBoolean Dim last_row AsLong, last_col AsLong, i AsLong, j AsLong Dim ws_header As Worksheet, ws_records As Worksheet Dim start_time AsSingle start_time = Timer
indent_space_space = 2' If use tab (Chr(9)) can reduce file size. need_trim = True Set ws_header = ThisWorkbook.Sheets(1) Set ws_records = ThisWorkbook.Sheets(2) xml_filepath = Left(ThisWorkbook.FullName, InStr(ThisWorkbook.FullName, ".") - 1) & ".xml" Open xml_filepath For Output As #1 Print #1, "<?xml version=""1.0"" encoding=""ASCII"" standalone=""yes""?>" Print #1, "<file>" Print #1, Space(indent_space) & "<header>" For j = 1To ws_header.Cells(1, Columns.Count).End(xlToLeft).Column tag = LCase(ws_header.Cells(1, j)) If need_trim Then val = Trim(ws_header.Cells(2, j)) Else val = ws_header.Cells(2, j) EndIf If val = ""Then Print #1, Space(indent_space * 3) & "<" & tag & "/>" Else Print #1, Space(indent_space * 2) & "<" & tag & ">" & val & "</" & tag & ">" EndIf Next Print #1, Space(indent_space) & "</header>"
Print #1, Space(indent_space) & "<records>" With ws_records last_row = .Cells(Rows.Count, 1).End(xlUp).Row last_col = .Cells(1, Columns.Count).End(xlToLeft).Column For i = 2To last_row Print #1, Space(indent_space * 2) & "<record>" For j = 1To last_col tag = LCase(.Cells(1, j)) If need_trim Then val = Trim(.Cells(i, j)) Else val = .Cells(i, j) EndIf If val = ""Then Print #1, Space(indent_space * 3) & "<" & tag & "/>" Else Print #1, Space(indent_space * 3) & "<" & tag & ">" & val & "</" & tag & ">" EndIf Next Print #1, Space(indent_space * 2) & "</record>" Next EndWith Print #1, Space(indent_space) & "</records>" Print #1, "</file>"
Close #1 Debug.Print "Transfer Success! Cost Time " & Timer - start_time & "s" ' MsgBox "Transfer Success! Cost Time " & Timer - start_time & "s" EndSub