tags:

views:

416

answers:

3

I need to create an Excel 2007 template workbook and devise the means to programmatically populate it with a dataset. The template workbook should include all of the formatting and boilerplate content that will appear in the final result, but will not inlcude any data rows.

The data injection process must be able to dynamically insert data into the workbook, hopefully without any knowledge of the styling, conditional columns, calculations, charts etc that are present in the template.

I have been experimenting with the new OpenXml Document Formats SDK 2.0 (using C#) and found that it offers a lot of flexibility in reading and manipulating Excel workbooks. However, the more I delve into it, the more I discover that it requires an intimate knowledge of the inner workings of Excel. For example, the fact that tables, worksheets and the calculation chain are all maintained separately inside the package is testing the limits of my understanding. I am concerned that my "solution" might exhibit unexpected behavior in different data scenarios.

So, I am wondering whether there is a more robust approach ...

Given that Excel is capable of binding to external data sources, including Xml data, is it possible that my dataset could be saved as an Xml file and injected into the workbook package file? In other words, is it feasible for an "external" data source to be inside the workbook package? If so, how would I go about defining a placeholder table in the template that would automatically bind to the injected data when the workbook is opened?

Many thanks for your suggestions.

A: 

Have you tried doing this with the 'Get External Data' that is built into excel? Data tab of ribbon then in the From Other Sources drop down you can have Excel retrieve information from you xml file. Some experimentation might be necessary to get the formatting down.

guitarthrower
Thanks for your reply. Yes, I think you're right that the only way to bind a worksheet to an Xml data source is as you describe. Unfortunately, the way Excel handles this is not very elegant - it just takes the data and places it in cells, which means that maintaining the data programmatically means iterating over the cells. I was really hoping for a method where the injected data would retain its original schema and simply be rendered in cells. But since I have been disappointed I have now started writing a class library that does this by adding an abstraction layer to the OpenXml SDK.
Tim Coulter
A: 

You could try creating a custom button (on the ribbon bar ?) to read the xml in, and populate the sheet for you.

VSTO is your friend there. :)

Nick Haslam
+1  A: 

This involves a bit of a different approach, but I've found Apache POI (http://poi.apache.org/) to be great for generating Excel workbooks. I have yet to find an Excel feature I wanted that wasn't supported. It also does not require a very deep knowledge of Excel and comes with a nice library of examples.

I, too, had to convert XML-based data to Excel. I wrote the code that used POI in Scala to take advantage of its wonderful XML-processing library.

It's surprising to see this question resurrected after such a long time, but I appreciate your feedback. I wasn't aware of Apache POI, but it looks very interesting. In fact, if I had known of its existence, I may not have had quite the same motivation to develop ExtremeML (http://www.extrememl.com/), but it's been an interesting journey and it's encouraging to know that a variety of solutions exists.
Tim Coulter