C# VSTO可在Excel中写入单元格!
为什么这样做有效:C# VSTO可在Excel中写入单元格!,c#,.net,excel,C#,.net,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循环。我该怎么做 我
((Excel.Worksheet)Application.ActiveSheet).get_Range("A1", "A1").Value2 = text;
但这并不是:
Excel.Worksheet activeSheet = ((Excel.Worksheet)Application.ActiveSheet);
activeSheet.Cells[0, 0] = text;
我需要使用第二种方法,因为我需要使用rowIndex和colIndex循环。我该怎么做
我得到一个错误:
类型的未处理异常
'System.Runtime.InteropServices.COMException'
发生在mscorlib.dll中
信息:HRESULT的异常:
0x800A03EC
除了使用基于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";
}
}
Excel.Worksheet-activeSheet=((Excel.Worksheet)Application.activeSheet);
int endRow=5;
int-endCol=6;
对于(int idxRow=1;idxRow+1),我只是想让读过这篇文章的人知道,填充大量单元格值的更快方法是使用对象[,],例如:使用c#4和“dynamic”关键字,现在可以只使用:dynamic cell=sheet.Cells[1,1];cell.Value2=“hi there”;更多-