Set cell value using Excel interop

Arthurdent510 picture Arthurdent510 · Jan 27, 2011 · Viewed 81.7k times · Source

Ok, so I'm trying to set the value of a cell with the excel interop library. I am able to do it with the following:

sheet.Cells[row, col] = value;

but it's terribly slow for how many I'm setting. So I'm trying to go this route:

Range excelRange = sheet.UsedRange;
excelRange.Cells.set_Item(row, col, value);

The code executes, but no data is put in the cell. Any suggestions on what I'm missing? Thanks!

Answer

Andy Mikula picture Andy Mikula · Jan 27, 2011

Your first method should work fine for any reasonable (and a lot of unreasonable) amounts of cells, provided you have disabled screen updating (Application.ScreenUpdating = false). The Knowledgebase Article describing how to set cells using C# accesses by row and column as well.