从Actions窗格中读取Excel单元格 [英] Read Excel Cell from Actions Pane

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

问题描述

我正在尝试从ActionPane中的代码读取工作表上的单元格值。到目前为止,这就是我所拥有的:


 

 Excel.Worksheet wk =(Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet; 
int rowCount = wk.UsedRange.Rows.Count;
for(int i = 0; i< rowCount; i ++)
{
try
{
MessageBox.Show((wk.get_Range(wk.Cells [ i,1],System.Type.Missing).Text.ToString()));
}
catch(例外e)
{
MessageBox.Show(e.Message.ToString());
}
}



 



 但是,当我运行此代码时,我收到一条错误消息"HRESULT异常..."

解决方案

您确定该单元格包含文本吗?如果内容是文本,Text属性只能返回一些内容。

尝试Range对象的Value2属性。

您也可以先尝试创建Range对象,然后再查询其属性。或许更像这样?

Excel.Range rng = wk.get_Range(wk.Cells [i,1],System.Type.Missing);
MessageBox.Show(rng.Value2的ToString());

I am trying to read the values of cells on a worksheet from code within an ActionPane. So far here's what I've got:

 

Excel.Worksheet wk = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
int rowCount = wk.UsedRange.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
try
{
MessageBox.Show((wk.get_Range(wk.Cells[i, 1], System.Type.Missing).Text.ToString()));
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}

 

 However, when I run this code I get an error message "Exception from HRESULT..."

解决方案

Are you sure that the cell contains text? The Text property can only return something if the content is text.

Try the Value2 property of the Range object.

And you might also try first creating the Range object, then querying its property. More like this, perhaps?

Excel.Range rng = wk.get_Range(wk.Cells[i, 1], System.Type.Missing);
MessageBox.Show(rng.Value2.ToString());


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

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