(对象[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)导致转换错误 [英] (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault) causes a conversion error
问题描述
错误:
无法将类型'字符串'到'对象[*,*]
这是错误我已经获得。有人可以给我一些指点,使我能避免吗?谢谢你。
注意:
有趣的是,(对象[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)
只会产生这个错误时, range.Count == 1
。它正常工作时数是等于或高于2
样品code:
对象[,] arrValue; //全局变量
私人字符串[] createFormulaCollection()
{
ArrayList的S =新的ArrayList();
尝试
{
//看当前活动的Excel工作表
//遍历细胞(不为空),并找到一个包含IES(...)
//保存到数组列表
//使用字典保存位置和值(位置键)
工作簿= Globals.ThisAddIn.Application.ActiveWorkbook;
工作表= Globals.ThisAddIn.Application.ActiveSheet;
范围= worksheet.UsedRange;
的MessageBox.show(range.Count.ToString());
如果(range.Count→1)
{
//需要转换为对象之前,要确保至少有2IES细胞[,]
arrValue =(对象[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
}
其他
{
arrValue [1,1] = range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault); //我尝试在这里。似乎仍然有问题,但。
}
赶上(例外前)
{
}
返回(串[])s.ToArray(typeof运算(字符串));
}
查找:
range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
将返回一个字符串时range.Count == 1
这意味着它不能转换为对象[,]类型。
是可以的,但是,当range.Count> 1
我的解决方法:
只是对付它分开。所以,在我的情况下,我只好先计算范围对象的数量,并相应地处理它们。
如果(range.Count→1)
{
// code ...
}
其他
{
字符串singleStrValue = range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
INT iRow,ICOL;
iRow = range.Row;
ICOL = range.Column;
如果(!string.IsNullOrEmpty(singleStrValue))
{
// code ...
}
}
Error:
Cannot convert type 'string' to 'object[*,*]'
That's the error I have been getting. Can someone give me some pointers so that I can avoid it? Thanks.
Note:
Interestingly, (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)
will only produce this bug when range.Count == 1
. It works fine when count is equal to and above 2.
sample code:
object[,] arrValue; //global variable
private string[] createFormulaCollection()
{
ArrayList s = new ArrayList();
try
{
//look at the currently active excel sheet
//iterate through cells (not Null) and find the one contains IES(...)
//save it into the arraylist
//use dictionary to save position and value (position as key)
workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
worksheet = Globals.ThisAddIn.Application.ActiveSheet;
range = worksheet.UsedRange;
MessageBox.Show(range.Count.ToString());
if (range.Count > 1)
{
//need to make sure there are at least 2 "ies" cells before converting to object[,]
arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
}
else
{
arrValue[1,1] = range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault); //my try here. seems still got problem though.
}
catch (Exception ex)
{
}
return (string[])s.ToArray(typeof(string));
}
Finding:
range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
will return a string when range.Count == 1
which means it can't be converted to object[,] type.
it can, however, when range.Count > 1.
My workaround:
Just deal with it separately. So in my case I had to first count the number of range object and process them accordingly.
if(range.Count > 1)
{
//code...
}
else
{
string singleStrValue = range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
int iRow, iCol;
iRow = range.Row;
iCol = range.Column;
if (!string.IsNullOrEmpty(singleStrValue))
{
//code...
}
}
这篇关于(对象[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)导致转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!