3

One can define ranges in C# Excel Interop like so:

var homeHomeOnTheRange = _xlSheet.Range[_xlSheet.Cells[3, 7], _xlSheet.Cells[42, 11]];

This range will encompass the subset of cells on the page from row 3 down to row 42, and across from columns 7 (or "G") through 11 (or "K").

What, though, if I want a "jagged" range - is it possible to concatenate an array of ranges into one range?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

2

You can use this format sheet.Range("A1:A2,B2:B3,C3:C4").

Pay attention that sheet should be of type dynamic so don't use Worksheet class for this purpose.

Example

Set the specified range background color to red:

var range = sheet.Range("A1:A2,B2:B3,C3:C4");
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398