Excel – Convert rows of spreadsheet to separate XML files

excelxml

I am looking for a system to convert a spreadsheet (or CSV file) with a few hundred rows of data into individual XML files. One column in the spreadsheet would be the required filename, and the other columns would contain data to be inserted into specific nodes in the XML based on a template.

Kind Regards,
Dan

Edit 1
If it can be handled totally within Excel, creating a new XML for each row and inserting the relevant column data into the correct location of the XML template that would be the best outcome.

Best Answer

Using VBA and the MSXML2.DOMDocument this could be achieved as follows:

Assuming you have a Excel sheet like this: enter image description here

Then VBA like this should create a XML file for each row:

Sub testXLStoXML()
 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + _
        "<data>" + vbNewLine + _
        "   <name>" + vbNewLine + _
        "   </name>" + vbNewLine + _
        "   <birthdate>" + vbNewLine + _
        "   </birthdate>" + vbNewLine + _
        "   <amount>" + vbNewLine + _
        "   </amount>" + vbNewLine + _
        "</data>" + vbNewLine

 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(1)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To lLastRow
   sFile = .Cells(lRow, 1).Value
   sName = .Cells(lRow, 2).Value
   sBirthdate = Format(.Cells(lRow, 3).Value, "YYYY-MM-DD")
   sAmount = Format(.Cells(lRow, 4).Value, "Currency")
   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("name")(0).appendChild doc.createTextNode(sName)
   doc.getElementsByTagName("birthdate")(0).appendChild doc.createTextNode(sBirthdate)
   doc.getElementsByTagName("amount")(0).appendChild doc.createTextNode(sAmount)
   doc.Save sFile
  Next

 End With
End Sub