views:

820

answers:

3

I've got a load of DataTables that I want to export into several Excel worksheets (in a new spreadsheet / Excel file). The "solution" that already exists in the code I have is two nested for loops, iterating over all the rows and columns, inserting data cell-by-cell. This isn't ideal as the process takes well over 10 minutes. There's a fair amount of data as the resulting spreadsheet is 8.5MB, but surely there must be a faster way?

I'm using the Office Interop libraries to do this; I think I remember reading somewhere that you can import CSV with these. If this is the case, would it be quicker to turn the DataTables into CSV and then import this into Excel, and if so, how?

+2  A: 

Your best bet is probably to speed things up by using the Value property setter on the Range object. It accepts an array, which works much faster than setting each cell individually. You can see a sample of this at http://support.microsoft.com/kb/302096.

John Fisher
A-ha, looks good. I'll give this a go and see what happens...
Graham Clark
A: 

Is this thru a .aspx page?

Sai Ganesh
Ah, no, winforms. I'll add a tag...
Graham Clark
A: 

SpreadsheetGear for .NET has an API similar to Excel's but will drop your time from 10+ minutes to a few seconds (actually, generating an 8.5MB workbook would typically take <1 second on a modern server). SpreadsheetGear also includes an IRange.CopyFromDataTable method which might work for you.

You can see live ASP.NET samples (C# & VB) here and download the evaluation here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
Wow, looks really good, but this is just an internal tool, so can't really justify the price.
Graham Clark