简体   繁体   中英

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.

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.

You can speed this up by treating the 1000 rows with 8 to 10 columns as an array and copy your data into this array in one go instead of filling each one of those 8000 to 10000 cells on its own. See here for more info: Write Array to Excel Range

Have you considered using C# tools such NPOI .
It works very well and it doesn't require you to use COM Interop.
Scott Mitchell wrote about it recently .

I wouldn't use an Excel app to do this, instead write it directly from c# using a library: Create Excel (.XLS and.XLSX) file from C#

MUCH faster.

Rgds GJ

Sometimes it's sufficient to save your files as a text file in csv format. It can be opened by Excel like a regular Excel sheet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM