|

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-and-drop operations to connect individual data fields from the map to the list source. Finally, the data is exported with a menu command.
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.

- 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.
- 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 <section> tag repeated twice deliberately; this allows Excel to infer that it will repeat throughout the list of data.

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

- In the XML Source task pane, click the XML Maps… button.

- 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:

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

This is a good time to verify Excel’s 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 <section> element in our example output file has been interpreted as a “repeating parent element”. The “repeating” aspect of this interpretation is necessary to correlate with repeating rows of list data. The element’s “children” are the individual fields of data across a given row.
- Now, it’s 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.

- 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.

- 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.
- 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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh) | FirstPrevNext |
|
 |
|
|
Hi Mike!
I've created a solution that is able to convert to XML without (almost) any limitations: Table2XML
It works by reverting the "flattener" methodology that is applied when Excel loads XML files. With a few changes to column order (and adding/deleting some columns) you have a very flexible way to convert tabular data into XML. The algorithm doesn't need any add-in (as MSXML), so it's easy to rewrite it in any other environment/language (I've already created a perl version here).
-regards
Roland
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Thanks for the feedback on previous question 
Has anyone been able to get excel to export time in hh:mm:ss format?
The output xml has produced it with fractions/frames (00:02:16.000) which I dont require.
I'm using type="xsd:time" in my schema, and have correctly formatted the cells within the excel sheet. I've also tried making xml to import as a schema with values in '00:00:00' and produces same result.
Any feedback appreicated, Ed
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Ed. I'm sorry, I don't know how to drop the .000 from the time specification. Is that simply a function of using the xsd:time type?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi there, the article has been a great help, but how could I get it add a </section> tag at the end of each section(row data) in the output XML? (before the next <section ....> starts)
Cheers!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
In the example I showed, you don't need an additional </section> closing tag, because the shorthand /> is already used to close the opening tag... like this:<section ... />
But if you want a syntax like <section ...></section> I think you could just modify the map file in step #2 accordingly.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I think that in order for the XML data to be exportable, the XML schema must be constructed as in your example with one element per row and all columns represented by attributes. I tried to do it using multiple elements and was not successful.
If you agree with that conclusion, do you know why that would be the case?
Thanks for the article as it ultimately helped me achieve my goal.
Cal Schrotenboer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have done this successfully with elements as well as attributes. For example, using the sample.xls file in the download, you could try a map file like this:<sections>
<section> <id></id> <call></call> <title></title> <instructor></instructor> </section>
<section> <id></id> <call></call> <title></title> <instructor></instructor> </section>
</sections> Every field is mapped to an element instead of an attribute, and the export works just fine.
I think using a bonafide schema file is best, for a variety of reasons, one of which is the fact that the schema will define an element like <section> as repeatable. If you're using an example output file instead, from which Excel must infer the schema, you need to make sure to repeat the element that corresponds to each list row (in this case, <section> ).
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
this is a good lesson for me-------xml beginner
i did not understand what difference it is between XML schame and XSL. would you like give me a simple example of when we need to use XML and when to use XSL for XML file please. just some education. i read some article and tutorial but did not have a chance to practise. thanks. if you do not mind.
Rock Stone
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
You may see interesting example of opening xml files for beginners here: http://server1.msn.co.in/computing/officexp/xml_excel.html
Alexander
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi there. If you're new to XML and related technologies (like XSL, XPath, etc...) you may want to run through the XML tutorials on http://www.w3schools.com[^].
Briefly, XML is a means for describing data - any kind of data. It is a plain text format, marked up with tags and attributes used to describe whatever you want to describe.
XSL stands for "XML Stylesheet Lanaguage", and it is a means for transforming source XML data into some other format. For example, one could use XSL to transform some source XML data into HTML for display on the web. Or you could use XSL to transform source XML data into a different format of XML. This is often used when different entities (businesses, organizations, institutions,...) are exchanging data with eachother. Business 1 has its own internal format for its data, Business 2 uses a different format internally. But if they agree on a form of XML to describe the data they would exchange, then they could exchange data freely. When Business 2 received the XML from the Business 1, Business 2 could run its own XSL process to transform that data into the data format Business 2 uses internally.
Take a look at the tutorials to get started.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|