Domanda

I need to convert a spreadsheet to a basic XML Format. The spreadsheet looks like this:enter image description here

I would like each row from the spreadsheet to look like this on the XML:

<item>
   <title>Directory Name</title>
   <address>Address, City, State</address>
   <phone>Phone</phone>
   <cell>Cell Phone</cell>
</item>

Any suggestions?

UPDATE: Per a few things I read, and per @Sancho.s suggested code I have used the following Macro:

Sub xls2xml()
    Dim rng1 As Range, rng2 As Range, cl As Range
    Set rng1 = Range("A2")
    Set rng1 = Range(rng1, rng1.End(xlDown))
    For Each cl In rng1
      Call dump_xml_line(cl)
    Next cl

End Sub

Sub dump_xml_line(cl As Range)
Dim n As Integer

n = FreeFile()
Open "C:\Users\Me\test.txt" For Output As #n


    Print #n, "<item>"
    Print #n, "   <title>" & cl.Text & "</title>"
    Print #n, "   <address>" & cl.Offset(0, 2).Text & "</address>"
    Print #n, "   <phone>" & cl.Offset(0, 1).Text & "</phone>"
    Print #n, "   <cell>" & cl.Offset(0, 3).Text & "</cell>"
    Print #n, "</item>"
    Close #n
End Sub

I had to use Print instead of Debug.Print because the Immediate Window isn't allowed to show more than 200 lines, and that is much much less than what the output of all this would be. Using this code, it creates the test.txt file, but only puts the very last row into the file, instead of all of them. What is wrong with it?

È stato utile?

Soluzione

This code should work for you. It is pretty much self-explanatory, and adaptation should be easy. To complement this code with other actions, you may find it helpful to record a macro, if you need to check what is the (approximate) code for a given task.

Sub xls2xml()
    Dim rng1 As Range, rng2 As Range, cl As Range
    Set rng1 = Range("A2")
    Set rng1 = Range(rng1, rng1.End(xlDown))
    For Each cl In rng1
      Call dump_xml_line(cl)
    Next cl
End Sub

Sub dump_xml_line(cl As Range)
    Debug.Print "<item>"
    Debug.Print "   <title>" & cl.Text & "</title>"
    Debug.Print "   <address>" & cl.Offset(0, 2).Text & "</address>"
    Debug.Print "   <phone>" & cl.Offset(0, 1).Text & "</phone>"
    Debug.Print "   <cell>" & cl.Offset(0, 3).Text & "</cell>"
    Debug.Print "</item>"
End Sub

Then you can copy the output from the immediate window.

Good luck.


EDIT To write to file instead of the immediate window, use the following (also self-explanatory) (see How can I write to a text file reliably from Excel VBA?)

Sub xls2xml_2()
    Dim rng1 As Range, rng2 As Range, cl As Range
    Set rng1 = Range("A2")
    Set rng1 = Range(rng1, rng1.End(xlDown))

    Dim strPath As String
    strPath = "test2.txt"
    Dim fnum As Integer
    fnum = FreeFile()
    Open strPath For Output As #fnum

    For Each cl In rng1
      Call write_xml_line_2(cl, fnum)
    Next cl

    Close #fnum
End Sub

Sub write_xml_line_2(cl As Range, fnum As Integer)
    Print #fnum, "<item>"
    Print #fnum, "   <title>" & cl.Text & "</title>"
    Print #fnum, "   <address>" & cl.Offset(0, 2).Text & "</address>"
    Print #fnum, "   <phone>" & cl.Offset(0, 1).Text & "</phone>"
    Print #fnum, "   <cell>" & cl.Offset(0, 3).Text & "</cell>"
    Print #fnum, "</item>"
End Sub

There is yet another option (see How to create and write to a txt file using VBA):

Sub xls2xml_1()
    Dim rng1 As Range, rng2 As Range, cl As Range
    Set rng1 = Range("A2")
    Set rng1 = Range(rng1, rng1.End(xlDown))

    Dim strPath As String
    strPath = "test1.txt"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
    Set oFile = fso.CreateTextFile(strPath)

    For Each cl In rng1
      Call write_xml_line_1(cl, oFile)
    Next cl

    oFile.Close
    Set fso = Nothing
    Set oFile = Nothing
End Sub

Sub write_xml_line_1(cl As Range, oFile As Object)
    oFile.WriteLine "<item>"
    oFile.WriteLine "   <title>" & cl.Text & "</title>"
    oFile.WriteLine "   <address>" & cl.Offset(0, 2).Text & "</address>"
    oFile.WriteLine "   <phone>" & cl.Offset(0, 1).Text & "</phone>"
    oFile.WriteLine "   <cell>" & cl.Offset(0, 3).Text & "</cell>"
    oFile.WriteLine "</item>"
End Sub

Altri suggerimenti

If your objective is to get the converted text, and it is not mandatory for you to use VBA (you did not specify this to be the case), then you could export your worksheet as csv, and use another program (say, in Objective C) to carry out the conversion.

It will take you a few more clicks, but if you want to "upgrade" your worksheet or the manipulation you carry out to get your XML in the future, then you may feel more comfortable with this option, which is fully functional.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top