Monday, May 19, 2008

Creating Open XML (OOXML) Spreadsheet Documents

As Office Open XML (OOXML) gains more wide-spread adoption and popularity - and since it is now an ISO standard - developers will be interested in how easy it is to create Open XML documents directly in their applications, e.g. spreadsheet documents that are compatible with Excel 2007. Most approaches require quite a bit of hand-coding and worrying about the actual OpenXML specifications, but what I want to show you today on the XML Aficionado blog is a way to use MapForce to auto-generate all the source-code (for example in C#) that will produce the desired .xlsx document so that you can integrate it into your applications (and use it royalty-free within your organization).

I will use a very simple example to demonstrate how you can turn some raw sales data in an arbitrary XML format:

SalesDataXML

into a pretty business graph in Excel 2007:

SalesDataGraph

For such a simple use-case you could, of course, simply open the XML file in Excel 2007 directly, but I am only using a simple example to illustrate the process. The true power of this approach is that you can easily work with very complex data in a visual and intuitive manner - and that you can auto-generate the source-code to implement this as part of your application to automate such processes.

So let's open MapForce and insert the XML data file into our working surface where we are going to define the mapping:

MapForceXMLfile

Next we are going to insert an OpenXML spreadsheet document into the work surface of our mapping project - we can either insert an empty spreadsheet, or we can use an example document that we have previously created in Excel to indicate what sheets and what data ranges or labels should be receiving our data:

MapForceExcelTarget

Now it is time to define how the source XML data should be mapped to the target OpenXML document. This particular mapping is just one example - MapForce lets you map between any combination of XML, relational database, EDI, flat-file (e.g. legacy text files), and OpenXML spreadsheet documents. In our case we are going to convert from start-date/end-date ranges in the XML source to months in the OpenXML document and from states to regions:

SalesDataMapping

Once you've defined the whole mapping, this is how your project will look in MapForce - note that underneath the blue-gradient working surface the "Mapping" tab is the one that is presently selected, because I've just defined my mapping between the input and output files:

MapForceScreenshot

To test my mapping - before I auto-generate my program code, I can click on the "Output" tab underneath the working surface, and MapForce opens up Excel 2007 embedded within the same application frame to show me the result that is produced by my mapping:

SalesDataExcelOutput

This Excel table is then used to produce the graph that I showed earlier.

Now I want to auto-generate code in C# for my data integration project that will automate this generation of Excel 2007 OpenXML documents, so the next step is to check the code-gen settings to ensure that I generate it for the correct development environment - in my case Visual Studio .NET 2008 - but MapForce supports many other environments and can also generate code in C++ or Java in addition to C#.

CSharpSetting

OK, now we are ready to generate code. All that is required is using the corresponding command on the File menu, and all the source-code files are placed in a designated output directory, and the corresponding solution file for Visual Studio is generated as well:

MapForceCodeGen

The auto-generated source-code can now be integrated into any application and can be used royalty-free within your organization to automate the creation of Open XML (OOXML) spreadsheet documents.

If you would like to experiment a bit more with this example yourself, you can find all the files used here in the MapForceExamples directory when you download the free 30-day evaluation version of MapForce.

Also, keep in mind that you can use Excel 2007 files (or any other OpenXML spreadsheet documents) in MapForce both as input and output files, so you can create data integration applications and mapping or conversion code for any possible scenario that involves OOXML spreadsheet data, XML, EDI, or relational databases.

No comments: