Click here to Skip to main content
6,629,377 members and growing! (19,106 online)
Email Password   helpLost your password?
Languages » VBScript » General     Beginner

Export Excel to XML in VBA

By Raymond Pang

VBA to generate a clean XML file from an Excel table provided with nested nodes support
VBScriptWin2K, WinXP, Win2003, Dev
Posted:3 May 2004
Views:125,383
Bookmarked:34 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.82 Rating: 4.28 out of 5

1
1 vote, 4.3%
2
2 votes, 8.7%
3
8 votes, 34.8%
4
12 votes, 52.2%
5

Sample Image - xls2xml.jpg

Introduction

In Excel XP, there is a new export option for XML, however, what I get is many many rubblish tags ( so disappointed..) . Then, after searching on the google (our nice search engine) for sometime about "Converting Excel to XML", what I can 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 codes contains 2 functions GenerateXMLDOM() and fGenerateXML(). They are actually doing the same thing in different approach,

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

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

Both functions has the same parameters , 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 worth to notice is we can use the node name to defines 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 equvalent to <data><field1>....</field1><data> , /student/name/surname is equvalent 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 equvalent 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 has to place inside the same sub-node should be place next to each other. For example, if /student/id and /student/name are place 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 check 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 is 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

About the Author

Raymond Pang


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.
Occupation: Web Developer
Location: Hong Kong Hong Kong

Other popular VBScript articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralThanks Pinmemberajalilqarshi0:36 7 May '09  
QuestionChange the default value for rootNodeName [modified] Pinmembertasmaniac14:46 28 Apr '08  
GeneralHelp with VB Pinmemberdommydee@hotmail.com3:36 19 Oct '07  
Questionxml conforming to DTD Pinmembermeghz1:09 8 May '07  
Generalgeneralized conversion to XML Pinmemberrkapl11:35 7 Jan '07  
GeneralNot support xsd PinmemberPerlDev3:39 6 Oct '06  
Generalexcel project deployment Pinmemberfatih isikhan4:59 11 Apr '06  
GeneralRe: excel project deployment PinmemberPerlDev3:34 6 Oct '06  
GeneralRe: excel project deployment Pinmemberfatih isikhan4:21 6 Oct '06  
GeneralCompile Error Pinmemberslefever9:07 1 Dec '05  
GeneralRe: Compile Error PinmemberRaymond Pang6:10 6 Dec '05  
GeneralRe: Compile Error PinmemberDavid Daltonhurst10:30 1 Jul '09  
Generalexport txt to xls PinmemberAunalisiraj23:07 12 Oct '05  
GeneralAttributes? PinmemberMani Shankar0:42 26 Aug '05  
GeneralRe: Attributes? PinmemberRaymond Pang22:20 27 Aug '05  
GeneralAttributes! PinmemberMani Shankar1:33 29 Aug '05  
GeneralRe: Attributes! Pinmemberthaylin795:40 28 Nov '06  
GeneralRe: Attributes? Pinmemberkevingreiner3:02 10 Jan '06  
Generalgood explanation. PinsussR.Poonkothai14:50 10 Aug '05  
GeneralGood Job... Thanks!!! Pinmembertechnoguys16:59 18 Feb '05  
GeneralRe: Good Job... Thanks!!! PinmemberRaymond Pang18:49 20 Feb '05  
GeneralThanks Pinmemberbanka_ravi1:24 21 Jan '05  
GeneralVBA of Excel PinmemberWillian.BR10:55 5 May '04  
GeneralRe: VBA of Excel PinmemberRaymond Pang15:46 5 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 May 2004
Editor:
Copyright 2004 by Raymond Pang
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project