gpt4 book ai didi

c# - 无法对空引用异常执行运行时绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 20:11:40 25 4
gpt4 key购买 nike

我试图通过查找第一列(由日期组成)和标题行中的第一个空单元格来使用 C# 查找 Excel 表格的维度。

这是我现在使用的代码:

public static void findingTableBounds()
{
string dateCol = "";
ArrayList dateColumn = new ArrayList();
ArrayList numberOfColumns = new ArrayList();

for (int column = 1; column < currentRow; column++)
{
dateCol = ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2.ToString();
if (dateCol != "")
{
dateColumn.Add(dateCol);
currentRow++;
totalRow++;
Console.WriteLine("Total Row: {0}", totalRow);
}
else
{
Console.WriteLine("Total Row: {0}", totalRow);
currentRow = 2;
}
}

**注意:这个方法有一个右括号,我没有包括它,因为还有另一个 for 循环与上面的代码做完全相同的事情,但只针对有多少列。

错误发生在“dateCol = ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2.ToString();”我很确定它会发生,因为当字符串是不可为空的类型时,我试图将空值(单元格)分配给 dateCol(字符串)。不幸的是,我不确定如何解决这个问题。

最佳答案

将空值应用于字符串是可行的,但如果((Excel.Range)workSheet.Cells[currentRow, 1]).Value2 为 null,则它没有函数 ToString() 不是函数,因此尝试执行它不会不工作。它是否说明了它是什么类型的异常?因为可能会有更大的问题...

关于c# - 无法对空引用异常执行运行时绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4939760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com