XML and Excel
XML in Excel
Let's get a bit more practical and use Excel to do something with XML files. With Excel 2003/2007 importing XML data is very simple. Unfortunately, the XML facilities are only available in Excel 2007 and in the Microsoft Office Professional or ultimate versions of Excel 2003, or when Excel 2003 is bought as a standalone package. When you select File (Office button in Excel 2007), Open from Excel, then you will see xml file types are shown along with the available Excel files:

Figure 2: The File, Open window of Excel 2007.
Selecting the file test-en.xml causes a small window to appear (see figure 3).

Figure 3: Excel asking what to do with the XML file
By selecting the third option Excel will analyse the structure of the XML file and present the structure in the taskpane (figure 4). Excel does not open the XML file at this moment.

Figure 4, The structure of the xml file shows up in the taskpane
The elements of the XML file can now be tied to cells on the sheet, simply by dragging them from the task pane to a cell in Excel. See Figure 5 and 6.

Figure 5: Dragging an element to an Excel cell

Figure 6: Result after dropping the element
By dragging an element which contains multiple other elements (or by selecting multiple elements in the taskpane using control+click or shift+click) all elements are placed next to each other as a list:(see figure 7).

Figure 7: List produced by dragging the "employee" element to the sheet
Now a template has been defined which can import data from XML files which have the same structure as the one we "opened" in Excel. We can import the data from test_en.xml, simply by pressing the refresh button on the "External Table Data" group of the custom "Table tools, design" tab of the ribbon in Excel 2007, or by clicking the Refresh XML data button on the List toolbar of Excel 2003 (see figure 8 and 9). The data from test_en.xml will be read into our newly created template. See figure 10.

Figure 8: Refresh Data using the ribbon

Figure 9: Button "Refresh XML data"on the "List" toolbar

Figure 10: Result of importing the data
To import a different file into the same structure, use the connection properties dialog, which you can access by clicking the dropdown arrow below the refresh button (see figure 11):

Figure 11: Changing connection properties to Import another XML file into Excel
After clicking connection properties, click the Details tab on the dialog that shows up and you can hit the browse button to access a different XML file. Or -in Excel 2003- Simply press the "Import XML" button on the List toolbar.






Comments
Showing last 8 comments of 31 in total (Show All Comments):Comment by: David (2/23/2010 1:10:36 PM)That appears to have taken care of the issue. Thank you for all your help.
Comment by: per jensen (2/25/2010 11:30:58 AM)hi karel, is it posible to change any attribs in the xmlmap object inside excel. know that editing the external xml file and then use update will do the thing.
Comment by: Jan Karel Pieterse (2/26/2010 7:21:29 AM)Hi Per,
Not through the userinterface.
In VBA, you can change the XML belonging xml maps inside an Excel file. The property you are looking for is:
Comment by: Anton Coetzee (3/24/2010 11:37:02 PM)Thank you for sharing your knowledge on your website.
My Question:
I have a .dotm file with a custom Ribbon. I have a function that dumps the contents of each object in the project (i.e. forms, modules, etc.) to a text file which I can then use to compare to a previous version of my .dotm file to see what's changed.
It would be useful to also include the customUI.xml (my ribbon customisation) content in this text file. How do I get access to the contents of the customUI.xml component (as text) of my .dotm file using VBA?
Comment by: Jan Karel Pieterse (3/25/2010 1:26:21 AM)Hi Anton,
Please have a look at this page:
www.jkp-ads.com/Articles/Excel2007FileFormat03.asp
Comment by: Jeff Weir (5/3/2010 7:34:26 PM)Hi Jan. I've set up a web query in Excel 2007 that pulls an XML RSS feed into the spreadsheet.
I noticed that when you set the query up, the XML Properties dialog box doesn't have a Refresh EVERY X MINUTES
option. Even if you go Data/Connections/Properties later, and under the USAGE tab set refresh to say 1 minute, it still doesn't refresh the XML web query.
Is this what you would expect?
To get around this, I used this VBA
'
' Refresh Macro
'
'
Debug.Print "Data refreshed at " & Now
'
' This is where we put everything needed to refresh the data
'
' Schedules this procedure to be run 15 minutes from now
Application.OnTime Now + TimeValue("00:15:00"), "Refresh"
ThisWorkbook.XmlMaps("rss_Map").DataBinding.Refresh
Worksheets("Sheet1").Range("Table1[#All]").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 7 _
, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18), Header:=xlYes
End Sub
In case it's helpful for some reason, here's the RSS where my web query points to: rss.seek.com.au/rss.aspx?catlocation=1017&Keywords=&RM=main&RMO=model-nzmain&RUS=SEEKNZMAIN_USR&RID=66624ccf-f543-4cd3-ab8b-e99a5df9b089
Thanks for any help you can offer.
Comment by: Jan Karel Pieterse (5/4/2010 12:35:31 AM)Hi Jeff,
As far as I know, indeed xml maps do not have a timed refresh option built-in. Your VBA code seems to be the best way to work around this.
Comment by: Jeff Weir (5/4/2010 12:49:45 AM)Thanks for that Jan...I really appreciate it when someone like yourself takes time out of your no doubt busy day to help someone else out.
Kind regards
Jeff
Have a question, comment or suggestion? Then please use this form.
If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.