VSTO写入Excel中的单元格!
为什么这个工作:
((Excel.Worksheet)Application.ActiveSheet).get_Range("A1", "A1").Value2 = text;
但是这不是:
Excel.Worksheet activeSheet = ((Excel.Worksheet)Application.ActiveSheet); activeSheet.Cells[0, 0] = text;
我需要做的第二种方式,因为我需要循环rowIndex和colIndex。 我怎样才能做到这一点?
我得到的错误:
mscorlib.dll中发生未处理的“System.Runtime.InteropServices.COMException”types的exception其他信息:HRESULT的exception:0x800A03EC
唯一缺less的东西(除了使用基于1的索引之外)是将单元格引用强制转换为一个范围:
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"; } }