Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Export Excel to XML in VBA

0.00/5 (No votes)
3 May 2004 5  
VBA to generate a clean XML file from an Excel table provided with nested nodes support
Sample Image - xls2xml.jpg

Introduction

In Excel XP, there is a new export option for XML. However, what I get is many rubbish tags (I'm so disappointed...). Then, after searching on the Google (our nice search engine) for some time about "Converting Excel to XML", what I get are conversion tools that simply make a Rational table to XML. However, what I want is the support of nested structures (e.g. <data><field1>....</field1><data>). As a result, I decided to write my own using VBA... (don't ask me why VBA... may be I will port it to my favorite C++ platform later. :)

Using the Code

The source code contains 2 functions GenerateXMLDOM() and fGenerateXML(). They are actually doing the same thing in a different approach.

GenerateXMLDOM -- Gives a Microsoft XMLDOM object which you can play with or serialize into an XML at any time using the "Save" method.

fGenerateXML -- Generates a long string which is ready for output as file (more readable, as proper newline is added).

Both functions have the same parameters. The first one is rngData, which is the selected table area in the Excel Sheet for conversion. The second (rootNodeName) is the first/root node's name used in the XML.

In the selected range, the first row is expected to be the field (or node) names. One thing that is worth noticing is that we can use the node name to define the level it belongs to. Started and separated by the node delimiter "/", the node name is one level deeper than the previous one. e.g. /data/field1 is equivalent to <data><field1>....</field1><data>, /student/name/surname is equivalent to <student><name><surname>.... </surname></name></student>:

'
' rngData : The selected region on Excel sheet, with the first row as field name,
' and data rows below
'  For the field name, use the node delimiter "/" to build the hierarchy of data
'  e.g. /data/field1 is equivalent to <DATA><FIELD1>....</FIELD1><DATA>
'
' rootNodeName : The XML document root node tag name

Function GenerateXMLDOM(rngData As Range, rootNodeName As String)
...


Function fGenerateXML(rngData As Range, rootNodeName As String) As String
...

Limitation and Notes

The ordering of fields may affect the generated XML, fields that have to be placed inside the same sub-node should be placed next to each other. For example, if /student/id and /student/name are placed next to each other, it will generate:

<student><id>..</id><name>...</name></student> 

However, if not, the result will be:

<student><id>..</id></student> <somebrabra...> ... </somebrabra...>
<student><name>..</name></student> 

The reason is that it only checks with the last field instead of all before deciding where the node should be created.

Finally I would like to thank Hasler Thomas, the author of A Filebrowser for VBA, who provided the code for file browse. Hope this code will be useful for you. Please let me know if there are any bugs.

History

  • 4 May 2004 -- First release 0.8 version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here