Fastest way to write cells to Excel with Office Interop?

calendar_today Asked Jun 22, 2010
thumb_up 24 upvotes
history Updated April 16, 2026

Question posted 2010 · +11 upvotes

I am writing a function to export data to Excel using the Office Interop in VB .NET. I am currently writing the cells directly using the Excel worksheet’s Cells() method:

worksheet.Cells(rowIndex, colIndex) = data(rowIndex)(colIndex)

This is taking a long time for large amounts of data. Is there a faster way to write a lot of data to Excel at once? Would doing something with ranges be faster?

Accepted answer +24 upvotes

You should avoid reading and writing cell by cell if you can. It is much faster to work with arrays, and read or write entire blocks at once. I wrote a post a while back on reading from worksheets using C#; basically, the same code works the other way around (see below), and will run much faster, especially with larger blocks of data.

  var sheet = (Worksheet)Application.ActiveSheet;
  var range = sheet.get_Range("A1", "B2");
  var data = new string[3,3];
  data[0, 0] = "A1";
  data[0, 1] = "B1";
  data[1, 0] = "A2";
  data[1, 1] = "B2";
  range.Value2 = data;

Excel VBA objects referenced (4)

  • Application — Using events with the Application object
  • Application — Working with Other Applications
  • Worksheet — Refer to All the Cells on the Worksheet
  • Worksheet — List of worksheet functions available to Visual Basic

Top excel Q&A (6)

+24 upvotes ranks this answer #22 out of 167 excel solutions on this site — top 13%.