我使用以下代码从Excel电子表格中读取值:
// Start with passed
int lastPassRow = sheets[passedVehicles].GetLength(0);
for (int i = 1; i < lastPassRow; i++)
{
// Exception here
if(DateTime.TryParse(sheets[passedVehicles][i, 0].ToString(), out result))
{
passedDates.Add((DateTime)sheets[passedVehicles][i, 0]);
}
}
sheets[passedVehicles]
的类型是Object[,]
的多维数组,上面的for循环在i = 1时给出了IndexOutOfRange
异常,尽管我检查了行数。
我为相关电子表格添加了一些日志记录,并已经验证:
lastPassRow
的值为4 sheets[passedVehicles].GetLength(1)
的值也是四。所有值似乎都在我的范围内。还有其他可能导致此异常的事情吗?
注意:我从i = 1开始,因为第0行是电子表格中的标题,不包含我想要阅读的数据。
答案 0 :(得分:4)
我怀疑0
超出了范围。
Excel数组是基于1的,而不是像您期望的那样基于0。