Introduction
ExcelExportXML
is a Microsoft Excel 2010/2015 Add-in that generates XML data from an Excel sheet. It is developed in Visual Studio using C#.
It is a very simple add-in. To understand how it works, consider an Excel sheet having the following data.
Country | Capital | Continent |
France | Paris | Europe |
Germany | Berlin | Europe |
India | New Delhi | Asia |
Indonesia | Jakarta | Asia |
Using the add-in, the above can be exported to the following XML.
<sheet1>
<row>
<country>France</country>
<capital>Paris</capital>
<continent>Europe</continent>
</row>
<row>
<country>Germany</country>
<capital>Berlin</capital>
<continent>Europe</continent>
</row>
<row>
<country>India</country>
<capital>New Delhi</capital>
<continent>Asia</continent>
</row>
<row>
<country>Indonesia</country>
<capital>Jakarta</capital>
<continent>Asia</continent>
</row>
</sheet1>
Once the Add-in is installed, you will have a 'Generate XML' button on Add-ins tab of Excel Ribbon as shown below. Clicking 'Generate XML' will pop up the save file dialog which will allow you to save the generated XML file.
Use Case & Assumptions
ExcelExportXml
is useful when you have tabular data in Excel and the first row contains the column headings. It works based on the following assumptions:
- The first row is considered as column headers and will be converted to XML tags.
- After encountering the first empty cell in header row, rest of the columns to the right will be ignored.
- Supports columns up to '
Z
' only, i.e., maximum of 26 columns. - The first row for which all values are empty will be considered the end of the sheet.
- The sheet name and column names should not have any spaces.
Why Not Use Standard Save As XML Functionality
Excel provides more than one way to export XML data. One of them is 'Save As XML Data' available in Save As dialog. This requires XML Mappings to be defined which I believe requires developer Add-in from Microsoft to be installed.
Another option is 'Save As XML Spreedsheet 2003' which generates the following XML:
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="5" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Column ss:Width="51"/>
<Column ss:Width="54"/>
<Column ss:Width="51.75"/>
<Row ss:StyleID="s64">
<Cell><Data ss:Type="String">Country</Data></Cell>
<Cell><Data ss:Type="String">Capital</Data></Cell>
<Cell><Data ss:Type="String">Continent</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">France</Data></Cell>
<Cell><Data ss:Type="String">Paris</Data></Cell>
<Cell><Data ss:Type="String">Europe</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">Germany</Data></Cell>
<Cell><Data ss:Type="String">Berlin</Data></Cell>
<Cell><Data ss:Type="String">Europe</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">India</Data></Cell>
<Cell><Data ss:Type="String">New Delhi</Data></Cell>
<Cell><Data ss:Type="String">Asia</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">Indonesia</Data></Cell>
<Cell><Data ss:Type="String">Jakarta</Data></Cell>
<Cell><Data ss:Type="String">Asia</Data></Cell>
</Row>
</Table>
</Worksheet>
As you can see, the above might not be what you want. It is more a representation of Excel sheet than semantics of your data. ExcelExportXML
is quite limited but effective in a common scenario where you have tabular data and the first row contains column headers.
Writing Add-in for Microsoft Excel 2010
Using Visual Studio 2010 Professional, creating Excel Add-in is pretty simple. Following are the steps I followed for ExcelExportXml
:
Step # 1
Create a new project of type 'Excel 2010 Add-in'. The project will have a class file called ThisAddin.cs.
Step # 2
For creating a button on the Excel ribbon, right click on the project and select Add - > New Item.
Step # 3
Select 'Ribbon (XML)' from the list of new items and name the ribbon class, say Ribbon1
. This will add two files to the project which are Ribbon1.cs and Ribbon1.xml.
Step # 4
Add the following code in ThisAddin
class:
protected override Microsoft.Office.Core.IRibbonExtensibility
CreateRibbonExtensibilityObject()
{
return new Ribbon1();
}
Step # 5
In Ribbon1.xml, define the properties of the button that will be displayed on the Excel ribbon. The onAction
attribute specifies the method which will be called from Ribbon1.cs on clicking the button (onAction="OnTextButton"
).
="1.0"="UTF-8"
<customUI xmlns=http://schemas.microsoft.com/office/2009/07/customui
onLoad="Ribbon_Load">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="Export XML">
<button id="textButton" label="Generate XML"
screentip="Export to XML" onAction="OnTextButton"
supertip="Export excel sheet to XML file."/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Step # 6
Add the following method declaration to Ribbon1.cs and implement your functionality here.
public void OnTextButton(Office.IRibbonControl control)
{
}
History
- 21st January, 2015: Limit of number of columns removed. Unlimited number of columns can be exported now.
- 7th December, 2011: Initial post