0

我正在遍历从范围中获得的行,以便在单元格内容中找到特定单词,然后我想获取找到它的列。例如,如果我在第 19 位找到所需的内容,则表示 excel 列是“S”。

这是我到目前为止使用的代码:

Excel.Worksheet xlWorkSheet = GetWorkSheet(currentWorkBook, "sheet");
var row = xlWorkSheet.Rows["5"];
int rowLength = xlWorkSheet.UsedRange.Columns.Count;
Excel.Range currentTitle = row[1]; //in order to iterate only over the 5th row in this example
  for (int i = 1; i < rowLength; i++)
  {
    string title = currentTitle.Value2[1,i];
    if (title == null)
    {
      continue;
    }
    if (title.Contains(wordToSearch))
    {
      string column = THIS IS THE QUESTION - WHAT DO I NEED TO WRITE HERE?
      Excel.Range valueCell = xlWorkSheet.Range[column + "5"];
      return valueCell.Value2;
    }

注意string column我需要添加代码的行。

4

1 回答 1

1

至于您不想依赖任何计算,我看到的另一个选项是从Address属性中提取列字母,如下面的代码所示:

Excel.Range currentRange = (Excel.Range)currentTitle.Cells[1, i];
string columnLetter = currentRange.get_AddressLocal(true, false, Excel.XlReferenceStyle.xlA1, missing, missing).Split('$')[0];
string title = null;
if (currentRange.Value2 != null)
{
     title = currentRange.Value2.ToString();
}

如您所见,我强制出现“$”以简化列字母检索。

于 2013-08-12T15:17:19.653 回答