Reafidy's edited answer is a great start, but I wanted to expand on it more than I could do in a comment. sheet.get_Range(rangeselect)
is much faster than going row by row, but one thing I haven't seen mentioned yet is that the get_Range parameter has a 255 character limit.
To get around that limitation, construct a set of ranges like "8:8,10:13,14:55" as normal then use a variant of this code:
string rangeSelectPart;
while (rangeSelect.Length >= 255)
{
rangeSelectPart = rangeSelect.Substring(0, rangeSelect.Substring(0,255).LastIndexOf(','));
Range multiRangePart = sheet.get_Range(rangeSelectPart, Type.Missing);
//do something with the range here using multiRangePart
rangeSelect= rangeSelect.Substring(rangeSelectPart.Length + 1);
}
Range multiRange = sheet.get_Range(rangeSelect, Type.Missing);
// do the same something with the last part of the range using multiRange
// now that the remaining rows are described in less than 255 characters
This will be significantly faster than doing operations on individual rows, but also won't fail when presented with large non-contiguous row sets.
Note that SutharMonil's answer is way faster IFF setting values in contiguous rectangular ranges. The bottleneck going from C# to excel is usually the repeated calls through the COM objects which block while being created and updated, and his answer nicely consolidates calls.
Unfortunately in my testing so far, trying to use it to work with non-string properties that aren't of type string has resulted in a type error. For example:
object[,] colors;
//use C# to set appropriate colors to each location in array...
for(int i = 0; i < colors.get_Length(0); i++){
for(int j = 0; j < colors.get_Length(1); j++){
colors[i,j] = XlThemeColor.xlThemeColorAccent6;
}
}
//below causes a type error
formatRange.Interior.ThemeColor = color;
I'll try to remember to update if I get it to work.
Lastly for repeated operations set Globals.ThisAddIn.Application.ScreenUpdating = false;
and then set it to true when you're done. Without this, Excel stops to update the screen after each set of range properties is updated and that can add a lot of time to the operation.