VSTO write in a cell in Excel!

Why does it work:

((Excel.Worksheet)Application.ActiveSheet).get_Range("A1", "A1").Value2 = text;

But this is not so:

Excel.Worksheet activeSheet = ((Excel.Worksheet)Application.ActiveSheet);
activeSheet.Cells[0, 0] = text;

I need to do this in a second way, as I need to loop with rowIndex and colIndex. How can i do this?

I get an error message:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll. More information: Exception from HRESULT: 0x800A03EC

+3
source share
1 answer

The only thing you are missing (other than using 1-based indexes) is direct reference to the cell reference:

Excel.Worksheet activeSheet = ((Excel.Worksheet)Application.ActiveSheet);
int endRow = 5;
int endCol = 6;

for(int idxRow = 1; idxRow <= endRow; idxRow++)
{
    for(int idxCol = 1; idxCol <= endCol; idxCol)
    {
        ((Excel.Range)activeSheet.Cells[idxRow, idxCol]).Value2 = "Kilroy wuz here";
    }
}
+10
source

Source: https://habr.com/ru/post/1708238/


All Articles