Click here to Skip to main content
Click here to Skip to main content

Export Excel to XML in VBA

By , 3 May 2004
 
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Raymond Pang
Web Developer
Hong Kong Hong Kong
Member
I am fond of programming especially in C++ (but unluckily I know many other different lang.). My special interest is on Graphics and image processing.
 
After graduated from Chinese University of Hong Kong with a research degree about graphics, I starts my career on web programming and games. Please Visit http://www.raybase.com which has software and game projects done by me.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanks! Great script!memberVladislav Gasan3 Jan '13 - 0:35 
QuestionNeed vba code for xml to excelmembersyed mubeen ahmed23 Jun '12 - 9:59 
Questionxml to excelmembersyed mubeen ahmed23 Jun '12 - 9:47 
Hi ,
 
If I have the excel with xml node, can I also put data from xml to excel as per the node.
 
Please provide me a macro for this....
 
Thanks in advance...
QuestionNeed help creating repeating group nodes using this toolmemberDan Gruber1 Sep '11 - 5:12 
Questionloop through all worksheets in a workbook tmemberushasharma198425 Jul '11 - 0:08 
GeneralStill helpful in 2011memberEelze13 Apr '11 - 23:28 
GeneralEXCEL HELP!!!!memberMRMIG14 Jul '10 - 20:09 
GeneralThanksmemberajalilqarshi6 May '09 - 23:36 
QuestionChange the default value for rootNodeName [modified]membertasmaniac28 Apr '08 - 13:46 
GeneralHelp with VBmemberdommydee@hotmail.com19 Oct '07 - 2:36 
Questionxml conforming to DTDmembermeghz8 May '07 - 0:09 
Generalgeneralized conversion to XMLmemberrkapl7 Jan '07 - 10:35 
GeneralNot support xsdmemberPerlDev6 Oct '06 - 2:39 
Generalexcel project deploymentmemberfatih isikhan11 Apr '06 - 3:59 
GeneralRe: excel project deploymentmemberPerlDev6 Oct '06 - 2:34 
GeneralRe: excel project deploymentmemberfatih isikhan6 Oct '06 - 3:21 
GeneralCompile Errormemberslefever1 Dec '05 - 8:07 
GeneralRe: Compile ErrormemberRaymond Pang6 Dec '05 - 5:10 
GeneralRe: Compile ErrormemberDavid Daltonhurst1 Jul '09 - 9:30 
Generalexport txt to xlsmemberAunalisiraj12 Oct '05 - 22:07 
QuestionAttributes?memberMani Shankar25 Aug '05 - 23:42 
AnswerRe: Attributes?memberRaymond Pang27 Aug '05 - 21:20 
GeneralAttributes!memberMani Shankar29 Aug '05 - 0:33 
GeneralRe: Attributes!memberthaylin7928 Nov '06 - 4:40 
AnswerRe: Attributes?memberkevingreiner10 Jan '06 - 2:02 
GeneralRe: Attributes?memberKevinGreiner25611 Aug '11 - 11:28 
GeneralRe: Attributes?memberMember 87030618 Mar '12 - 8:52 
Generalgood explanation.sussR.Poonkothai10 Aug '05 - 13:50 
GeneralGood Job... Thanks!!!membertechnoguys18 Feb '05 - 15:59 
GeneralRe: Good Job... Thanks!!!memberRaymond Pang20 Feb '05 - 17:49 
GeneralThanksmemberbanka_ravi21 Jan '05 - 0:24 
GeneralVBA of ExcelmemberWillian.BR5 May '04 - 9:55 
GeneralRe: VBA of ExcelmemberRaymond Pang5 May '04 - 14:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 4 May 2004
Article Copyright 2004 by Raymond Pang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid