Search code examples
c#performanceexcelcom-interop

How can I speed-up creating excel Excel spreadsheets from C#?


I am currently writing around 200 Excel spreadsheets using Excel 2007 with C# COM Interop.

Unfortunately I need about 4 minutes to write these 200 sheets - each sheet has around 1000 rows with 8- 10 columns.

How can I speed up things?

My code looks like this basically:

var xlApp = new Excel.Application();
xlApp.DisplayAlerts=false;
foreach (x in z) {
   var wb = xlApp.Workbooks.add(XLWBATemplae.xlWBATWorksheet);
   var ws = (Worksheet)wb.Worksheets[1];
   //fill data into sheet
   wb.SaveAs(fileName);
   wbClose();
}

clarification:I am filling every cell individually right now

I will try out some of the suggestions from you and will then accept the best solution.


Solution

  • You can get rid of COM Interop altogether, by using EPPlus (here). This library creates Excel files by using the 2007 XML format, not the Excel COM interface, and is a lot quicker as a result.