Introduction to XML capabilities in Excel (Part 2)
Previously we exported data in Excel to XML after creating XML Maps from a schema. Here, we will do the opposite, which is to import data from XML into Excel. The same schema will be used. The data in XML however is slightly different, the data has been sorted alphabetically according to the name. This is just to show that the XML data may undergo manipulations and subsequently reloaded into Excel.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employees">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Age" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Employees> <Employee> <Name>Chong</Name> <Age>44</Age> </Employee> <Employee> <Name>Crystal</Name> <Age>25</Age> </Employee> <Employee> <Name>Elize</Name> <Age>29</Age> </Employee> <Employee> <Name>Hafiz</Name> <Age>23</Age> </Employee> <Employee> <Name>Rashidan</Name> <Age>23</Age> </Employee> </Employees>
Here we have a workbook named SortedEmployees. In Sheet1, we create an empty table with two column headers, Name and Age.
From Developer tab, we launch the XML Source pane, and add the schema Employees.xsd below. This will create the XML Map required for importing XML data and mapping to the worksheet.
The mapping can be done by dragging the XML element from the source pane onto the target cell. In this case, we drag the Employee element onto Name column header.
Now that mapping is done, we may begin importing data from the XML file into Excel. Click on the Import button under Developer tab, and open the XML data source, in this case Employees.xml.
Voila! It’s done.
It’s that easy to import and export data between Excel and XML. The tricky part is to write the XML schema definition to create the appropriate XML map. But it’s most likely a one time job, and the schema can be reused many times as long as there are no changes to the data format. Hopefully this article helps a little to demonstrate the XML capabilities of Excel, and inspire you to learn more about Excel, XML and XML Schema.
Introduction to XML capabilities in Excel
Microsoft Excel is a spreadsheet software that is part of Microsoft Office suite. By nature, spreadsheet software is extremely easy to use to do simple calculation and to present data in tabular format. Business environment almost always need some sort of spreadsheet program and usually Microsoft Excel is the top choice due to its powerful capabilities, features and programming support for heavy customization with VBA and VSTO.
Ability to export data to XML format is one of these capabilities. There are few reasons why one might want to export data to XML. One reason that I can think of right now, is for the ability to share data in a widely understood format. XML, being an open standard, is popular among developers as a format for data interchange, and there are plethora of programming libraries available that can manipulate XML. This article intends to show a simple example of exporting data from Excel to XML.
Let’s start with a simple table as below.
Here, we have a workbook named My Organization containing three separate worksheets Sheet1, Sheet2 and Sheet3. In Sheet1, we construct a table with caption Employees. The table has two columns, Name and Age, with a number of entries. If we now try to save the table as XML Data, an error message will be displayed.
The error message states that XML data cannot be saved as there are no XML mappings. Without XML maps, Excel does not know which cells to read data from, nor how to write the XML output. Hence, we have to create the appropriate XML map for the data. For this, we need to write XML Schema to define elements in an XML map. As a simple example, we will create a complex element named Employees which contains a sequence of another complex element Employee. As there are multiple Employee elements, it is a repeating element.
Each Employee element is a complex type itself, containing two simple elements, Name and Age. These two are simple elements because there are no more than a single occurrence of these elements inside each Employee. Which makes sense, because a person usually has only one name and one age.
To define these elements, the following XML Schema is written and saved as employees.xsd.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employees">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Age" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Now that we have our schema ready, we can create XML map. For this purpose, we open the XML Source Pane under Developer tab. The pane is visible on the right.
Within the source pane, we can click the button XML Maps… to add an XML map to the workbook. A dialog will appear, where we can click Add… button to select the XML Schema file we created before, which is employees.xsd. Click OK once done adding.
Maps that are added will be visible in the XML Source Pane in a form of element tree.
Now it is time to do the mapping. We may drag each element in the XML Source Pane onto the cells or we might right click them and assign range. For the latter approach, we might right click at Employee element, and assign the range that cover Name and Age table header i.e. B2:C2. Assigning a continuous range like this is possible, as Name and Age columns are next to each other, and follow the sequence defined in the XML map.
Clicking OK, all data in the table will be mapped to appropriate elements.
Now that we have successfully mapped XML data to appropriate elements defined in XML map, we can export the table to XML. This is done by clicking on Export button under Developer tab, or by choosing to save as XML data, as we had unsuccessfully tried previously. The resulting XML file contains the data in XML format, as below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Employees> <Employee> <Name>Hafiz</Name> <Age>23</Age> </Employee> <Employee> <Name>Elize</Name> <Age>29</Age> </Employee> <Employee> <Name>Chong</Name> <Age>44</Age> </Employee> <Employee> <Name>Crystal</Name> <Age>25</Age> </Employee> <Employee> <Name>Rashidan</Name> <Age>23</Age> </Employee> </Employees>
Now we can manipulate the data in XML format
In subsequent posts, I will show how to do simple manipulation of XML data and to import the data back into Excel. Hopefully this will give a clear view of XML capabilities in Excel, and the situation in which this feature will become useful.
To import data from XML file into Excel instead, proceed to Part 2.
Problem #2 in Project Euler requires us to calculate the sum of even Fibonacci values that are less than 4,000,000. For this purpose, it may be helpful to understand what Fibonacci series is.
The first two numbers in Fibonacci series are 0 and 1. Each following number is the sum of the previous two numbers. The formula can be written as below.
Hence, Fibonacci series is of the following sequence: 0, 1, 1, 2, 3, 5, 8, 13, …
Knowing this, solving Project Euler problem #2 is quite straight forward. The following C# code does the job.
using System;
namespace Euler2
{
/* Sum of even Fibonacci values that do not exceed 4,000,000 */
class Program
{
static void Main(string[] args)
{
int fibA = 0;
int fibB = 1;
int fib = 0;
int total = 0;
int remainder;
while (fib < 4000000)
{
Math.DivRem(fib, 2, out remainder);
if (remainder == 0)
{
total += fib;
}
fib = fibA + fibB;
fibA = fibB;
fibB = fib;
}
Console.WriteLine("Finding sum of even Fibonacci values under 4,000,000");
Console.WriteLine("Answer is {0}", total);
}
}
}
I’ve decided to write the above program in C# since I’ve already got Visual Studio C# Express open from solving Project Euler #1. Whatever language you decide to program in, it is important to make sure that the data type chosen can support large enough number to hold the answer to the problem without overflowing.
The first problem in Project Euler requires us to find the sum of multiples of three and five under 1000.
Ignoring theory of arithmetic progression, this problem can be solved easily using loop construction. For each integer under 1000, we check whether the number is a multiple of 3, and if it is, we accumulate the number. We do the same for multiples of 5 and 15. After the loop finishes, the answer to the problem is simply the total of sum of multiples of 3 and sum of multiples of 5, minus the sum of multiples of 15.
We need to minus the sum of multiples of 15 because 15 itself is a multiple of 3 and 5, hence had been counted twice.
The C# source code can be used to calculate the answer. If you see a heavy influence of C imperative programming in the code, that’s just because it’s where I come from. It’s possible to write a one-liner program that does the same in some programming language, but if you’re learning and looking for sharpening your problem solving skill, step-by-step instruction is just fine. Learning the language features and libraries, while necessary, may come later.
using System;
namespace Euler1
{
/* Add the natural numbers below one thousand that are multiples of 3 or 5 */
class Program
{
static void Main(string[] args)
{
int sumMultiplesOf3 = 0;
int sumMultiplesOf5 = 0;
int sumMultiplesOf15 = 0;
int remainder;
int total;
for (int i = 1; i < 1000; i++)
{
Math.DivRem(i,3, out remainder);
if (remainder == 0)
sumMultiplesOf3 += i;
Math.DivRem(i, 5, out remainder);
if (remainder == 0)
sumMultiplesOf5 += i;
Math.DivRem(i, 15, out remainder);
if (remainder == 0)
sumMultiplesOf15 += i;
}
total = sumMultiplesOf3 + sumMultiplesOf5 - sumMultiplesOf15;
Console.WriteLine("Add the natural numbers below one thousand that are multiples of 3 or 5");
Console.WriteLine("Answer is {0}", total);
}
}
}
You may submit the answer to Project Euler website and receive a note of congratulation
.
Snipping tool is one of the utilities available in Windows 7 that I use a lot. It’s a very convenient way to catch a snapshot of a region of the computer display, and save it as image. I normally save the image in Portable Network Graphic (.png) format, but other formats are available too i.e. JPEG, GIF and single file HTML. Snipping tool has improved my productivity a lot, compared to the traditional way of Print Screen, pasting to Paint, crop a region and save as, as I did before.
Now, coupled with Math Input Panel (also available in Windows 7), publishing math on the web gets much easier. All I need to do is to touchpad-write a math equation into Math Input Panel. The program will recognize the writing, and produce MathML for the equation that can be inserted into a number of software including Office, Opera etc. However, Internet Explorer does not support MathML out of the box, so I’ll have to be content with using Snipping tool to cut the nice math display part.
This approach however does not seem like an optimum way to publish math on the net especially for serious math writer. In my case however, this approach is quick enough and hassle free, and quite importantly, can be done quickly with Windows 7 without needing to install additional software.
Do you recognize the following math formula?
7 New Year Resolutions for Lowly, Ineffective People
This time last year, I made a ridiculously ambitious set of new year resolutions. I was aware of the evil of making unrealistic goals, but I thought I was different. I thought could achieve anything. Afterall I was young and energetic. But unfortunately it turned out that I failed every single resolution including going to the gym, practicing my Mandarin, memorizing Hiragana/Katakana, earning money online, reaching 6d in Go strength, earning golf ‘licence’ and whatnot.
This year, I decide to only have a modest set of resolutions to fulfill. It may not be half as fun, but hopefully will be more meaningful and effective as a guide. Failing self-drawn resolutions every year is too meaningless to make a habit of.
Anyway, I decide on the following seven resolutions to welcome the new year, and the new decade ( if millennium started in 2000, decade may start from zero too). They are aimed at nurturing good habits and subtly improve my effectiveness as an individual.
Always carry a USB memory stick , pen/paper and business card with me everywhere. It’s not that I know the exact purpose these are for, but there were countless of occasions when I wished I had these. Especially business cards. It is too unprofessional to walk around without bringing them.- Read less.
That’s quite misleading, so I rephrase. Balance reading with doing actual work and producing result. Great men of the past may have been scholars i.e. Aristotles, Socrates etc, but great men of the present are survivors of harshest battle. Why else would we look up to dropouts? - Write more. No matter how little it is at a time. Rewriting whatever that I read is the easiest way to be productive. Make that a habit enough, it’ll get myself to be more result-oriented than effort-accounting-oriented.
- Ensure quality in work. The main aim is reusability, and is highly applicable with regards to programming and writing. Quality programming exercise must be readily publish-able. Study notes should be easily made into lecture notes or article. What can be more harmful to effectiveness than having to redo the same things again and again?
- Make use of free available resources. It’s time for me to be more saving-savvy. I spent too much on books last year when free resources are available. Why buy programming language syntax rule books when the standards are readily available? Books are most useful when they contain experience and advices from the expert, rather than being a prettier form of available manuals.
Relax. Listen to music. Two recent fantastic movies had one thing in common. They mentioned that when men are too full in their thoughts, they cant be receptive to ideas or think creatively. What are the two movies? Hint: Awesome cinematography.
- Continually assess the above 6 targets. As I set out above as my this year resolutions, clearly I have not actually done them. Best intentions aside, the above resolutions might be too light to create a measurable impact. Adjust as necessary if the set out resolutions do not work the way you want. There’s no need to be a purist and force yourself to adhere to painful resolutions no matter how original they are.
I notice that except for the first one, all of them could be too abstract but I am outlining them as the initiatives to combat my ineffectiveness. I am clear of my goals and aspirations and do not need a new year resolution as reminder. What I am more concerned about is my ability to convert my effort and investment into tangible results. Actually, I believe that is what everyone should be concerned about.


