Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top