A while back on my previous blog I wrote an entry about how to export data to Excel using ADO.NET. It used SQL commands to create the spreadsheet and then used SQL INSERT commands to export the data . The sample code on that post was only written to document the approach so that I wouldn't forget it.
I used this technique in a rewrite of a stress calculator. The application had to process an extremely large amount of raw data stored in a number of different Excel spreadsheets. The application did some heavy calculations on the data and then exported all the results to another Excel spreadsheet. Prior to the rewrite the export used OLE automation and took maybe 20 minutes to complete, after the rewrite took roughly 15-30 seconds.
The code was originally written in such a way that it was specific to the data it exported, not good, but yesterday I had the chance to polish it up and to abstract it out so that it could be used by any project. You'll find it attached to the post.
All you need to do is call....
ExcelExporter.Export(myDataView, mySpreadsheetPath)
*update 18th July 2006, turns out there was a bug in the class. It has been fixed! The problem was the class only handled System.Double and System.Integer number types and didn't include System.Int16, System.Int32, and System.Decimal. Everything's working fine with it now.*
If anyone finds problems with the code I post then please do tell me so I can fix it.