Search code examples
c#exceloffice-interopexcel-interop

Faster way to access Range using Excel.Interop


There are two ways I know-

_xlWorksheet.Range[_xlWorksheet.Cells[1, 1], _xlWorksheet.Cells[10, 10]].Value2 = myarray.ToArray();

OR

_xlWorksheet.Range["A1", "J10"].Value2 = myarray.ToArray();

OR

Is there any other faster way?

As per my understanding, when I use

_xlWorksheet.Range[_xlWorksheet.Cells[1, 1], _xlWorksheet.Cells[10, 10]]

there will be three calls to interop. But, in case of

_xlWorksheet.Range["A1", "J10"]

there will be only one call.

I am not sure which one works faster.


Solution

  • As far as I understand your question there is no "FAST" way when choosing .Range[_xlWorksheet.Cells[1, 1], _xlWorksheet.Cells[10, 10]] or Range["A1", "J10"] They are the same.

    In Excel, you can refer to a range, say A1:A10, in some of these ways

    Debug.Print Sheets("Sheet1").Range("A1:A10").Address
    Debug.Print Sheets("Sheet1").Range(Sheets("Sheet1").Range("A1"), Sheets("Sheet1").Range("A10")).Address
    Debug.Print Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, 1), Sheets("Sheet1").Cells(10, 1)).Address
    

    Choosing one of the above will NOT determine the performance. What WILL determine the performance is HOW you read/write to them