【问题标题】:Cannot perform runtime binding on a null reference - Empty Excel Cells无法对空引用执行运行时绑定 - 空 Excel 单元格
【发布时间】:2015-01-30 11:41:30
【问题描述】:

我似乎想不出一种方法来纠正标题中提到的错误,并且正在寻找一些关于应该做什么的想法。

我正在尝试将 excel 电子表格的行读入一个对象。

第一次循环时我没有问题,因为第 1 行、第 1 列和第 1 行第 2 列中有数据。

但是当它到达第 2 行、第 1 列和第 2 行第 2 列时,它会因上述错误而崩溃,因为电子表格中的那些单元格是“空的”

我只是想不出我可以在哪里放置一些“如果为空”检查。

谁能建议一下怎么做?

这是我的代码...

private static void IterateRows(Excel.Worksheet worksheet)
    {
        //Get the used Range
        Excel.Range usedRange = worksheet.UsedRange;

        // create an object to store the spreadsheet data
        List<SPREADSHEETModel.spreadsheetRow> spreadsheetrows = new List<SPREADSHEETModel.spreadsheetRow>();

        //Iterate the rows in the used range
        foreach (Excel.Range row in usedRange.Rows)
        {
            for (int i = 0; i < row.Columns.Count; i++)
            {
                spreadsheetrows.Add(new SPREADSHEETModel.spreadsheetRow()
                {
                    col1 = row.Cells[i + 1, 1].Value2.ToString(),
                    col2 = row.Cells[i + 1, 2].Value2.ToString()
                });
            }
        }
    }

【问题讨论】:

    标签: c# .net excel vsto


    【解决方案1】:

    不要使用.ToString(),当值为空时会导致null reference exception。 使用Convert.ToString(),它将返回空字符串作为空值。

    col1 = Convert.ToString(row.Cells[i + 1, 1].Value2);
    col2 = Convert.ToString(row.Cells[i + 1, 2].Value2);
    

    【讨论】:

    • 如果我使用.length?Convert.ToString(row.Cells[i + 1, 1].Value2).Length;???
    【解决方案2】:

    在致电ToString 之前,您需要它们。也许你甚至可以在添加之前移动 if ,因为我认为添加空行没有用,但在你的场景中这可能不是真的:

    if (row.Cells[i + 1, 1].Value2 != null && row.Cells[i + 1, 2].Value2 != null)
    {
        spreadsheetrows.Add(new SPREADSHEETModel.spreadsheetRow()
        {
            col1 = row.Cells[i + 1, 1].Value2.ToString(),
            col2 = row.Cells[i + 1, 2].Value2.ToString()
        });
    }
    

    否则这可能是你需要的:

    col1 = row.Cells[i + 1, 1].Value2 != null ? row.Cells[i + 1, 1].Value2.ToString() : null,
    

    异常背后的原因是Value2dynamic,所以返回值是在运行时确定的。而如果Value2null,则无法确定要调用的ToString方法。

    【讨论】:

    • 如您所见,我的情况是第 2 行第 3 列中有我想要获取的数据。共有17列:(
    • @TrevorDaniel:好的,然后看第二个代码示例。
    • 如果你经常使用它,你甚至可以在一个方法中拆分这个逻辑。
    • @TrevorDaniel:不客气。你为什么撤回你的投票/接受?我错过了什么?
    • @TrevorDaniel:这正是我告诉你的。第一句话提到ToString,最后一段解释了为什么错误与您通常看到的NullReferenceException不同...
    【解决方案3】:

    您可以在 for 循环中检查:

        //Iterate the rows in the used range
        foreach (Excel.Range row in usedRange.Rows)
        {
            for (int i = 0; i < row.Columns.Count; i++)
            {
                spreadsheetrows.Add(new SPREADSHEETModel.spreadsheetRow()
                {
            if (row.Cells[i + 1, 1].Value2 != null)
            {                       
                col1 = row.Cells[i + 1, 1].Value2.ToString();
            }
            if (row.Cells[i + 1, 2].Value2 != null)
            {
                            col2 = row.Cells[i + 1, 2].Value2.ToString();
            }
            if (row.Cells[i + 1, 3].Value2 != null)
            {
                            col3 = row.Cells[i + 1, 3].Value2.ToString();
            }
                });
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多