Many times I have to work on generating an Excel sheet and there was a time when the user reported that the generation of Excel reports was quite slow. While researching that I tried some Google searches and discovered that there's a way to write a large volume of data to excel very quickly. After I did the R&D on the code I thought to share it.
Here we'll not discuss how to create an Excel sheet programmatically but we'll see how we can write to Excel a large amount of data in the quickest manner possible. Use the trick mentioned below and impress your management.
Here's goes the code to create an Excel workbook and worksheet:
Microsoft.Office.Interop.Excel.Application xlApp = null;
Workbook xlWorkbook = null;
Sheets xlSheets = null;
Worksheet xlNewSheet = null;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook = xlApp.Workbooks.Open(sourcefile, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing,
false, XlPlatform.xlWindows, Type.Missing,
true, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// xlWorkbook = xlApp.Workbooks.Add(Type.Missing);
xlSheets = xlWorkbook.Sheets as Sheets;
// The first argument below inserts the new worksheet as the first one
xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlNewSheet.Name= "DummySheet";
Now here's the discussion of what to do after you have accomplished the preceding to create a sheet.
Most of the time we write a loop and then write the data cell by cell. And this makes the application go slower.
If you have your data in a collection then your best method is to write the data using a range.
var startCell = (Range)xlNewSheet.Cells[2, 1];
var endCell = new object();
endCell = (Range)xlNewSheet.Cells[numbers.Items.Count, 6];
var writeRange = xlNewSheet.get_Range(startCell, endCell);
writeRange.set_Value(Type.Missing, retList);
and after that release everything and quit so you don't loose your data.
xlWorkbook.Save();
xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
xlApp.Quit();
and finally release your COM objects.
finally
{
if(xlNewSheet!=null)
Marshal.ReleaseComObject(xlNewSheet);
if (xlSheets != null)
Marshal.ReleaseComObject(xlSheets);
if (xlWorkbook != null)
Marshal.ReleaseComObject(xlWorkbook);
if (xlApp != null)
Marshal.ReleaseComObject(xlApp);
xlApp = null;
KillExcel();
}
Happy Reporting :)).

PradeepPosted Jun 12, 2018, 2:24 AM
There's no info on what exact type of collection is retList? Excel Interop throws when using string[][] and object[][] with this method.
Sr KarthigaPosted Feb 26, 2016, 9:14 AM
good one
Sr KarthigaPosted Feb 26, 2016, 9:14 AM
nice explanation
Amin SayededitedPosted Mar 16, 2011, 7:09 AMEdited Mar 16, 2011, 7:11 AM
Not at all useful, if the Microsoft Office is not installed on the Server which happens in 99.99% cases.
Mahesh ChandPosted Mar 14, 2011, 8:12 PM
I think it belonged to the Office Interop category.
Amit ChoudharyPosted Mar 14, 2011, 7:56 AM
Yes good catch suthish.. but actually first i was writing for the both but i was more focused more on "Write Excel" part and just skipped the "Read" part. Well, the part i missed was basically to take the advantage or OleDbDataReader over the OleDbDataAdapter. Since the Reader objects are faster then Adapter(use to fill the datatable/dataset) if you are using the OleDb to read the Excel. Thanks.
Suthish NairPosted Mar 14, 2011, 6:17 AM
Reading part is missing?