You are on page 1of 6

6/9/12

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject


Email Password Sign in Join Lost password?

8,929,015 members and growing!

Home

Articles

Quick Answers

Discussions

Zones

Features

Help!

The Lounge

Search site

Languages C / C ++ Language General

Converting a List of Data to XML using Microsoft Excel 2003


By Mike Ellison | 23 May 2006 | Article
XML Windows Visual-Studio Dev Intermediate

Licence First Posted 23 May 2006 Views 92,433 Bookmarked 47 times

See Also
More like this More by this author

A tutorial on using XML maps in Excel to convert spreadsheet data to XML.


Article Browse Code Stats Revisions Alternatives
4.66 (23 votes) 19 Hot News: Signs that you're a good programmer The C ode Project Insider. Free each morning.

Download sample files - 2.58 Kb

Introduction
Microsoft Excel 2003 supports XML in a variety of ways. One application of this XML functionality is the ability to export a list of data to an XML format. This is particularly useful when working with data originally in comma-separated value files (.csv files) or fixed-width data files. Both are plain text file formats, and are common import/export options for database and spreadsheet applications. Normally, an XSL stylesheet would be considered when transforming a data source to an XML output. An XSL stylesheet will not help us transform .csv or fixed-width data into XML however, as XSL requires an XML-based source. A .csv or fixed-width file is easily imported into Excel though, and with a few additional steps, may be exported to an XML format of choice. An Excel list becomes exportable to XML when assigned an XML map. An XML map is an XML schema or other file showing an example of the desired XML output, in which case Excel can infer the schema. Either is associated with the spreadsheet containing the list data. The user then may perform drag-anddrop operations to connect individual data fields from the map to the list source. Finally, the data is exported with a menu command.

Related Articles
C onverter for Microsoft Project 2002 XML file to TODOLIST XML file C onvert Excel to XML file/XML Schema, and validate Excel against XML Schema Fetching Data from Microsoft Excel using SQL Excel Add-in for Exporting Data to XML Save data to Excel file using Microsoft Excel List C ontrol Data Binding using XML

Step-by-step
The starting point for this task is straightforward: Excel is open, and you have either entered or imported a list of data (e.g., a .csv file). Include a heading row, identifying field names at the top of each column. For practice, you may use the file Sample.xls which may be downloaded with the link at the top of this article.

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

1/6

6/9/12

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject


Restoring Two-Way Synchronization on SharePoint Lists Using Excel Using C # and Open XML SDK 2.0 for Microsoft Office to C reate an Excel 2007 Document WordML Templates Editor data visualization using Office 2003 WordML features A Very Easy to Use Excel XML Import-Export Library Generate Excel files without using Microsoft Excel C onvert your Data to Excel Spreadsheet How To Set An Appointment Using Microsoft Office Outlook 2003 Excel Report from Microsoft Project Write Data to Excel using C # Exporting Data to Excel Excel C onverter To Microsoft SQL SERVER C onvert XML data to object and back using serialization

1. We will begin by creating the map file, which can be either an XML schema or a file showing example XML output. Open Notepad or your desired text editor. 2. Enter and save the map file. For practice, you may use the file sectionsMap.xml that comes with the article download. In this illustration, we are creating an example XML document that demonstrates the output we wish to achieve rather than use a schema. Excel will read the example output and infer a schema appropriate for the map. For the example output, create a root element. Nested within the root, create an element that is associated with a single row of data in the list. The individual fields of data in the list row may be identified as attributes of that element, or as child elements of the parent. Once the element representing a full list row is complete, copy and paste it as a second element within the root. It is important that the example output imply multiple elements to associate with multiple list rows. The picture below shows the < e t o >tag repeated twice deliberately; this scin allows Excel to infer that it will repeat throughout the list of data.

ExcelML Templates Editor data visualization using Office 2003 SpreadsheetML features Microsoft Office XML formats, defective by design?

If the < e t o >tag were not repeated in the example output, Excel would treat its association scin as a single cell of data rather than a column of cells. Repeating the < e t o >element in the scin example allows Excel to infer an association to the column across multiple rows. 3. In Excel, with the sheet containing your list of data active, choose the menu Data -> XML -> XML Source. The XML Source task pane opens.

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

2/6

6/9/12

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject

4. In the XML Source task pane, click the XML Maps button.

5. In the XML Maps dialog, click Add..., and select the schema or XML example output file you created in step #2. If you are using an XML example rather than a schema, like the sectionsMap.xml file from the article downloads, you will see the following dialog, which you may dismiss by clicking OK:

6. In the XML Maps dialog, the map file will be added. Click OK to dismiss the dialog. 7. The XML Source task pane will now display the map elements and attributes.

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

3/6

6/9/12

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject


This is a good time to verify Excels interpretation of the example output. Each of the icons in the XML map describes the interpretation of the given element or attribute.

(source: Excel Help) The picture above shows that the < e t o >element in our example output file has been scin interpreted as a repeating parent element. The repeating aspect of this interpretation is necessary to correlate with repeating rows of list data. The elements children are the individual fields of data across a given row. 8. Now, its time to associate an individual child element or attribute in the map with its list column in the source. Do this by clicking and dragging a field from the map to the list column header to which it should be associated. When dropped on the column header, the list column will highlight and is automatically converted to an Excel List object.

9. Repeat the drag-and-drop operation for each additional field, associating each field in the map with its respective column of list data by dropping it on top of the column header.

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

4/6

6/9/12

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject

10. With the XML associations made, it is time to export the data. Choose the menu Data -> XML -> Export and specify the location/filename to create the new XML file. 11. Open the newly created file in Notepad or another text editor to verify that the export has worked as desired.

Summary
A list of data in Excel, such as that which may be obtained from a .csv or fixed-width data file, may be exported to an XML format, provided an XML map has been assigned to the spreadsheet. An XML map may either be an XML schema describing the source data, or an XML document showing an example of the desired output format. In an example output file, a parent element is defined to correlate with a given list row. The parent element is repeated at least once in the example output, allowing Excel to infer its association to repeated rows in the list. Individual child attributes and elements may then be associated with respective list columns through a drag-and-drop operation, connecting the XML child to its related list field header. With the XML map in place and field items associated to columns of data, the data may be exported with the menu command Data -> XML -> Export. When converted to XML, the list data is then suitable for use as a source in an XSL transformation or any other XML application.

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


Mike Ellison I work for the University of Nevada, Las Vegas in the Office of Institutional Analysis and Planning. Among other things, our office is charged with the mission of deriving useful information in support of administrative decisionmaking from institutional data. Within the context of that mission, my office mates and I apply technology in the form of custom data processing applications, data extraction and analysis tools, reporting tools, relational databases, OLAP solutions, data warehousing, and data mining. Visit my blog at MishaInTheCloud.com
United States

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

5/6

6/9/12
Member

Converting a List of Data to XML using Microsoft Excel 2003 - CodeProject

Article Top

Sign Up to vote

Poor

Excellent Vote

Comments and Discussions


You must Sign In to use this message board. (secure sign-in) Search this forum Profile popups Noise Medium Layout Normal Refresh Nice Article but one doubt My vote of 5 My vote of 5 Excellent Re: Excellent Excellent one Re: Excellent one Generating XML in Excel (and other environments) without limitations Excellent clarification Re: Excellent clarification Time notation without frames Re: Time notation without frames closure of tags Re: closure of tags XML Attributes vs. Elements Re: XML Attributes vs. Elements [modified] excellent article for beginner-----thanks Re: excellent article for beginner-----thanks Re: excellent article for beginner-----thanks Last Visit: 18:00 31 Dec '99 General News Last Update: 1:32 9 Jun '12 Suggestion Question Bug Answer Joke Rant Admin aashish30 thewazz v3ks priyanka.pardeshi Mike Ellison Jagan_nathan_V Mike Ellison rkapl Leniel Mike Ellison edwoofer Mike Ellison edwoofer Mike Ellison Cal Schrotenboer Mike Ellison ff_lee7 NoSpamNsk Mike Ellison Per page 25 Go Update First Prev Next 21:18 27 Feb '12 11:23 20 Jan '12 11:55 25 Apr '11 23:10 17 Nov '10 5:49 18 Nov '10 5:58 8 Jun '09 8:14 8 Jun '09 5:07 9 Jan '07 20:22 29 Dec '06 6:35 2 Jan '07 3:37 25 Oct '06 13:31 6 Nov '06 6:43 24 Oct '06 7:04 24 Oct '06 20:13 6 Jun '06 5:44 7 Jun '06 13:44 30 May '06 9:17 31 May '06 5:25 2 Jun '06 1

Use C trl+Left/Right to switch messages, C trl+Up/Down to switch threads, C trl+Shift+Left/Right to switch pages.
P ermalink | A dvertis e | P rivac y | M obile Web0 1 | 2 .5 .1 2 0 6 0 4 .1 | L as t U pdated 2 3 M ay 2 0 0 6 L ayout: fixed | fluid A rtic le C opyright 2 0 0 6 by M ike E llis on E verything els e C opyright C odeP rojec t, 1 9 9 9 - 2 0 1 2 T erms of U s e

www.codeproject.com/Articles/14201/Converting-a-List-of-Data-to-XML-using-Microsoft-E

6/6

You might also like