(object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault) causes a conversion error

喜欢而已 提交于 2019-12-03 20:09:01

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...
    }
}

Does it work with

(object[,])range.get_Value(System.Reflection.Missing.Value);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!