VSTO写在Excel单元格! [英] VSTO write to a cell in Excel!

查看:720
本文介绍了VSTO写在Excel单元格!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这项工作:

 ((Excel.Worksheet)Application.ActiveSheet).get_Range(A1,A1)值2 =文字。
 

但这并不:

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

我需要做的,因为我需要循环使用rowIndex位置和colIndex第二种方式。我该怎么办呢?

我得到的错误:

  

类型的未处理的异常   System.Runtime.InteropServices.COMException   出现在mscorlib.dll附加   信息:从HRESULT异常:   0x800A03EC

解决方案

你错过的唯一的事(不是使用1基于索引除外)投单元格引用明确的范围:

  Excel.Worksheet activeSheet =((Excel.Worksheet)Application.ActiveSheet);
诠释endRow = 5;
INT endCol = 6;

对于(INT idxRow = 1; idxRow< = endRow; idxRow ++)
{
    对于(INT idxCol = 1; idxCol< = endCol; idxCol)
    {
        ((Excel.Range)activeSheet.Cells [idxRow,idxCol])值2 =基尔罗伊WUZ这里;
    }
}
 

Why does this work:

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

But this doesn't:

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

I need to do it the second way as I need to loop with rowIndex and colIndex. How can I do that?

I get the error:

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

解决方案

The only thing you're missing (other than using 1 based indexes) is to cast the cell reference to a range explicitly:

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";
    }
}

这篇关于VSTO写在Excel单元格!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆