我有以下方法:
public static object getValue(Excel.Worksheet sheet, int row, int col)
{
Excel.Range r = sheet.Cells[row, col] as Excel.Range;
//I've also tried using sheet.get_Range(cell, cell); here, with the same result
if (r.Row != row || r.Column != col)
{
//Why does this debug statement sometimes print?
Debug.Print("Tried to read (" + row.ToString() + ", " + col.ToString() + ") but got (" + r.Row.ToString() + ", " + r.Column.ToString() + ")");
}
return r.Value2;
}
根据我对 Excel.Worksheet.Cells[row, column] 的理解,我的代码不应该进入 if 语句。但是,当我重复调用 getValue 以读取多个单元格时,它返回的范围的行和列经常与我调用 Excel.Worksheet.Cells 的行和列不同。
示例输出:“尝试读取 (19, 1) 但得到 (56, 5)”
此外,如果我中断 if 语句,然后倒回我的执行点并再次运行 Excel.Worksheet.Cells,我会得到正确的范围。
为什么会这样?