Fastest way to get an Excel Range of Rows

前端 未结 5 519
青春惊慌失措
青春惊慌失措 2020-12-10 05:32

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

5条回答
  •  有刺的猬
    2020-12-10 06:18

    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.

提交回复
热议问题