In a VSTO C# project I want to get a range of rows from a set of row indexes.
The row indexes can be for example like \"7,8,9,12,14\".
Then I want the range
Here is the code you are looking for:
int startRow, endRow, startCol, endCol, row,col;
var singleData = new object[col];
var data = new object[row,col];
//For populating only a single row with 'n' no. of columns.
var startCell = (Range)worksheet.Cells[startRow, startCol];
startCell.Value2 = singleData;
//For 2d data, with 'n' no. of rows and columns.
var endCell = (Range)worksheet.Cells[endRow, endCol];
var writeRange = worksheet.Range[startCell, endCell];
writeRange.Value2 = data;
You can have entire range, be it 1 dimensional or 2 dimensional array of cells.
This method is particularly helpful while looping through the entire excel sheet and populating data where and when required.