3

エラー:

Cannot convert type 'string' to 'object[*,*]'

それは私が得ているエラーです。誰かが私にそれを避けることができるようにいくつかのポインタを与えることができますか?ありがとう。

ノート:

興味深いことに、(object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)

このバグは、の場合にのみ発生しrange.Count == 1ます。countが2以上の場合は正常に機能します。

サンプルコード:

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));
        }
4

2 に答える 2

2

発見:

range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);range.Count == 1の場合、文字列を返します。これは、object [、]型に変換できないことを意味します。

ただし、range.Count>1の場合は可能です。

私の回避策:

別々に扱ってください。したがって、私の場合、最初に範囲オブジェクトの数を数え、それに応じて処理する必要がありました。

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...
    }
}
于 2011-09-12T08:57:22.337 に答える
0

で動作しますか

(object[,])range.get_Value(System.Reflection.Missing.Value);
于 2011-09-12T05:48:55.007 に答える