如何按列名获取单元格值?

时间:2017-03-17 04:13:04

标签: c# excel

下面我的代码工作正常,但如何按列名指定?

for (int resultFiltered = 1; resultFiltered <= filteredRange.Areas.Count; areaId++)
{
  Range areaRange = filteredRange.Areas.Item(resultFiltered);
  string studentID = areaRange.Cells[1, 1].Value2; //How to specify column name?
}

1 个答案:

答案 0 :(得分:1)

您可以用列号替换列号。

示例:

object rangeObject = activeWorksheet.Cells[3, "W"];

Range range = (Range)rangeObject;

object rangeValue = range.Value2;

string cellValue = rangeValue.ToString();

或者如果这有帮助,在我访问的Excel文件中,我将单元格“W5”命名为“overflow5”:

Range topExcel = activeWorksheet.get_Range("overflow5", Type.Missing);

topExcel.Select();

string cellValue = topExcel.Value2.ToString();