Welcome to OpenXML Developer Sign in | Join | Help

Custom XML Mapping in SpreadsheetML

Article by - Sheela E.N, Sonata Software Limited

 

The XML mapping feature enables us to add arbitrary XML data structures and XML schema definitions to the workbook and map XML nodes to cells and cell ranges in the workbook. Once the mapping is set up, the application will be able to import and export XML data according to the schema.

 

Here, we will be discussing two methods of XML mapping in SpreadsheetML.  For these samples we will use Excel 2007 to set up the mapping. The first method is through the Developer Tab and the second method is through the Data tab of Excel 2007.

 

  1. Open Microsoft Excel 2007 and go to Developer tab à Click on Source tab.

 

Click on Developer tab à Click on Source tab

 

 

In the XML Source window, click button - “XML Maps”. This will open a window, which will guide you to select the required XML and mapping it to the spreadsheet.

 

 

           

Click on Add and select a XML file.

 

 

XML maps in the workbook.

 

 

At this point, spreadsheet will have the schema of the selected xml file.

 

The following XML files will be generated –

 

connections.xml: This will have the connection details.

<connections xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">

<connection id="1" interval="1" name="books" type="4" refreshedVersion="3" minRefreshableVersion="1" background="1" refreshOnLoad="1" saveData="1">

<webPr xml="1" sourceData="1" parsePre="1" consecutive="1" xl2000="1" url="C:\test\books.xml" htmlTables="1"/>

                   </connection>

</connections>

 

xmlMaps.xml: This file will have the schema of the xml file attached.

<MapInfo xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" SelectionNamespaces="">

  <Schema ID="Schema1">

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">

………..

………

    </xsd:schema>

  </Schema>

  <Map ID="1" Name="bookstore_Map" RootElement="bookstore" SchemaID="Schema1" ShowImportExportValidationErrors="false" AutoFit="true" Append="false" PreserveSortAFLayout="true" PreserveFormat="true">

    <DataBinding FileBinding="true" ConnectionID="1" DataBindingLoadMode="1"/>

  </Map>

</MapInfo>

 

Drag and drop the required xml elements to XLSX file from xml source. Click on the “Import” or “Refresh Data” button under Developer tab.

 

 

Now, drag and drop required XML elements to Spreadsheet from XML source and click on the “Import” button under Developer tab. This will get whole of the data from the XML file and bind it to the Spreadsheet.

 

Now, the following XML parts will be generated –

 

table1.xml: This will contain the details about the table, i.e. the connection ID used, Table column, XPATH of each column.

<table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" id="1" name="Table1" displayName="Table1" ref="A1:H4" tableType="xml" totalsRowShown="0" connectionId="1">

  <autoFilter ref="A1:H4"/>

  <tableColumns count="8">

    <tableColumn id="1" uniqueName="genre" name="genre">

<xmlColumnPr mapId="1" xpath="/bookstore/book/@genre" xmlDataType="string"/>

    </tableColumn>

    …….

    ………

   

    <tableColumn id="8" uniqueName="price" name="price">

<xmlColumnPr mapId="1" xpath="/bookstore/book/price" xmlDataType="double"/>

    </tableColumn>

  </tableColumns>

 <tableStyleInfo name="TableStyleMedium9" showFirstColumn="0"  showLastColumn="0" showRowStripes="1" showColumnStripes="0"/>

</table>

 

sheet1.xml.rels: This will contain the relationship reference.

<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.xml"/>

 

  1. Open Microsoft Excel 2007 and go to Data tab à Click “From other sources” à Click From XML Data Import as the option.

 

 

 

 

 

Here, checking the “XML table in existing worksheet” will generate the data as shown below and the XML generated internally are same as the ones specified in the first method.

 

XML Map Properties shown on selection of “XML table in existing worksheet” will be as follows –

 

 

 

If suppose at the ‘Import Data’, when the other selection is made, the properties to be selected are different and are illustrated as follows –

 

Select the “Existing worksheet” or “New worksheet”

 

 

This has an option to refresh the data for every time interval specified and also at the time of opening the file. On selecting “Refresh Every 2 minutes” option, data changes in the specified XML will be shown every 2 minutes.

 

On checking “Refresh data when opening the file” will query the XML file for the data when opening the Spreadsheet file and displays the refreshed data.

 

 

Data in Spreadsheet will look like below –

 

 

This will create “QueryTable” part in the Spreadsheet Open XML file and the following XML files will be generated –

connections.xml: this will contain the connection details with intervals, refresh and URL details.

 

<connection id="1" interval="2" name="books" type="4" refreshedVersion="3" minRefreshableVersion="1" background="1" refreshOnLoad="1" saveData="1">

    <webPr xml="1" sourceData="1" parsePre="1" consecutive="1" xl2000="1" url="D:\Project\DotnetProj\Test\xl_XML_Mapping\books.xml" htmlTables="1"/>

  </connection>

 

queryTable1.xml: This will contain the details of the connection used for the query.

<queryTable xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" name="books" refreshOnLoad="1" connectionId="1" autoFormatId="16" applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="1" applyPatternFormats="1" applyAlignmentFormats="0" applyWidthHeightFormats="0"/>

sheet1.xml.rels: This will contain the relationship reference for the sheet and “QueryTable”.

<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/queryTable" Target="../queryTables/queryTable1.xml"/>

 

By this method, using Query Table we can trigger a query at regular intervals as well as at the time of opening of the Spreadsheet. This will in turn refresh the data on the Excel Spreadsheet.

 

These two methods help in mapping external XML to an Excel Spreadsheet file.

Published Monday, October 15, 2007 4:23 AM by SanjayKumarM
Filed Under: ,

Comments

 

Stephane Rodriguez said:


Misleading article!

This article is guaranteed to disappoint anyone who will read it.

"Custom XML" has a special meaning in the context of Microsoft Office XML formats, it's not just "Custom" and "XML" put together.

"Custom XML" means to store custom XML parts inside documents, and bind their content to the spreadsheet's grid using regular XML techniques (most notably schemas and XPath).

What readers expect to read in this article is the technique describing how to bind an XML data source stored internally (I N T E R N A L L Y) in Microsoft Office XML files to their spreadsheet.

What instead you are describing is a feature that appeared in Excel 2003, the ability to bind an external (E X T E R N A L) XML data source to their spreadsheet.

In fact, perhaps your article should better explain that there is no documented way to achieve that "Custom XML" binding in Excel 2007, and that users/developers are left with Excel 2003's external XML data binding. I have the feeling that the fact there is no documented way to do that suggests there is no way to do that at all, but you'll have to ask that to someone in the Excel team to get a firm answer.

-Stephane Rodriguez

November 6, 2007 3:15 AM
 

brauliod said:

Snapshot / Images missing: It seems that the images has been lost, I cannot see the snapshots, could you please fix it?
November 7, 2007 5:59 AM
 

Sheela E N said:

I am quite sure of what "Custom XML" means in OOXML.

My article deals with incorporating external xml into worksheet and the way to achieve it.

This article definitely does not disappoint anybody in OOXML developer community. If anyone has issues/clarifications, they are always welcome to ask questions and get them clarified.

-Sheela
November 14, 2007 12:35 AM
 

Stephane Rodriguez said:


@Sheela,

Sheela said "I am quite sure of what "Custom XML" means in OOXML. "

FYI, here is an article about Custom XML : http://openxmldeveloper.org/articles/Mapping_Content_Controls_to_Custom_XML_Parts_using_Notepad.aspx. This article deals with Custom XML in the context of Word 2007 documents. I would expect your article, since it has "Custom XML" in the title to be the Spreadsheet counterpart, a topic which has not been covered by anybody so far here.

Is it the case? Of course not. Your article is grossly misleading, do yourself a favor and change the title. Here is a suggestion for a new title : "How to create a custom map of an external XML data source". Here is another : "Querying an external XML data source".


Sheela said "My article deals with incorporating external xml into worksheet and the way to achieve it."

This is what you say in the *last* sentence of the article. How about starting the article with exactly that?

As someone else also said, your images too are broken. I find it very hard to believe that a grossly misleading article with broken images did not disappoint. If it was posted on the codeproject website, it would be voted many 1s.

November 23, 2007 7:17 AM
 

Vasudeo said:

Dear Sheela

I am trying to open XML in the spreadsheet for the first time.

In XML Sopurce window I clicked "XML Maps" and opened window.  Can you please advice where can I find specimen of XML which I can select for Mapping it on the spreadsheet.   I should appreciate if you please specify which Foler I should go to and which  file name I should specifically select.  Any guidance to start me off will be greatly appreciated.  Thanks

Vasudeo
January 25, 2008 6:08 AM
 

Karo said:

Sheila, please change the title, because it's really misleading.
February 5, 2008 4:45 AM
 

MauricioG said:

When somebody is looking for REAL "Custom XML" Mapping comes here.
You should change the title or add information related to Excel Custom XML Mapping/Binding....
February 8, 2008 10:57 AM
Anonymous comments are disabled