【问题标题】:Microsoft.Office.Interop IndexOutOfRangeExceptionMicrosoft.Office.Interop IndexOutOfRangeException
【发布时间】:2017-08-03 13:41:01
【问题描述】:

下面的 IndexOutOfRangeException 不允许我的代码运行(它编译)。虽然我理解这种异常(数组索引等),但问题是,我想做的只是用单元格 B[excelrow] 中的值更新字符串 subsection2。出于某种原因,有一个索引越界异常对我来说没有意义。 subsection2 或 excelrow 都不是数组的一部分。我能想到的唯一数组是 excel 数组,但 excelrow 是一个值为 3 的整数,它应该更新到 B3 行,依此类推。 (我什至尝试过直接用 B3 更新,我得到了同样的错误)。

为了进一步帮助您了解上下文,这个名为 createsource 的方法将 Excel 电子表格和该工作表中的总行数作为输入。它执行下面的代码以输出一个二维数组,其中第一维包含每个新订单(每个不同客户)的 excel 索引,第二维是每个客户订购的商品数量。

代码方法如下:

private int[,] createsource(Microsoft.Office.Interop.Excel.Worksheet xlWorksheet, int totalRows) {

        String subsection = "";
        object subsection2 = "";
        int orders = 0;

        //figures out how many different pages there are going to be
        for (int n = 3; n < totalRows + 1; n++)
        {
            if (!(xlWorksheet.get_Range("B" + n.ToString()).Text == subsection))
            {
                subsection = xlWorksheet.get_Range("B" + n.ToString()).Text;
                orders++;
            }
        }
        MessageBox.Show(orders.ToString());
        int[,] source = new int[orders, 2];

        int excelrow = 3;
        subsection2 = xlWorksheet.get_Range("B" + excelrow.ToString()).Text;
        int i;

        for (i = 0; i < orders + 1; i++)
        {
            int j = 1;
            if (excelrow == totalRows + 1)
            {
                break;
            }

 //Out of bounds exception is found in the below if statement updating subsection2:

            if (!(xlWorksheet.get_Range("B" + excelrow.ToString()).Text == subsection2))
            {
                source[i, 0] = excelrow;
                //MessageBox.Show(xlWorksheet.get_Range("B" + excelrow.ToString()).Text.ToString());
                subsection2 = xlWorksheet.get_Range("B" + excelrow.ToString()).Text;
                excelrow++;
            }

            for (int iter = 0; iter < 1;)
            {
                if (excelrow == totalRows + 1)
                {
                    break;
                }
                if (xlWorksheet.get_Range("B" + excelrow.ToString()).Text == subsection2)
                {
                    excelrow++;
                    j++;
                }
                if (!(xlWorksheet.get_Range("C" + excelrow.ToString()).Text == subsection2))
                {
                    subsection2 = xlWorksheet.get_Range("C" + excelrow.ToString()).Text;
                    iter = 1;
                }
            }

            source[i, 1] = j;

        }

        MessageBox.Show(source[2, 0].ToString());
        return source;

    }

【问题讨论】:

  • 在新变量中获取xlWorksheet.get_Range("B" + excelrow.ToString()).Text 的值。另外,在try/catch 中关闭您的代码。在catch 中获取错误的完整描述:示例:catch (Exception ex) { string strError = ex.ToString(); }edit 您的问题以及错误的描述。
  • 哪一行准确会抛出异常?

标签: c# excel-interop indexoutofrangeexception


【解决方案1】:

我看到了问题。您将来源声明为:

int[,] source = new int[orders, 2];

...好吧,但是看看你的循环:

for (i = 0; i < orders + 1; i++)

... 后来输入:

source[i, 0] = excelrow;

好的,如果orders = 100,你声明了一个100长的数组,从0到99。然后你的循环,你从 0 到“小于 100+1”,也就是 0-100。当您到达最后一个循环时,您正在使用 i=100 的值,并尝试将其放入不存在的数组点中。

您需要将循环减少 1,或者将数组大小增加 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多