如何查找与当前单元格C#+ Excel相邻的单元格的值

我写了一个程序,find一个包含“客户:”的单元,程序成功运行。 问题是,我想直接在单元格旁边的单元格的值。 布局看起来像这样:

__________ _________ |Customer:| | Steve | |_________| |________| |Customer:| | John | |_________| |________| |Customer:| | Frank | |_________| |________| 

所以在这种情况下,我想要的值“史蒂夫”,“约翰”,“弗兰克”。 我怎样才能做到这一点?

谢谢,卢克

我的代码:

 public void gatherInfo() { string temp = ""; Excel.Range currentFind = null; Excel.Range firstFind = null; foreach (Excel.Worksheet sheet in excelWorkbook.Application.Worksheets) { try { // access cell within sheet Excel.Range excelCell = (Excel.Range)sheet.get_Range("A1", Type.Missing); currentFind = excelCell.Find("Customer:", Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing); while (currentFind != null) { // Keep track of the first range you find. if (firstFind == null) { firstFind = currentFind; } // If you didn't move to a new range, you are done. else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) == firstFind.get_Address(Excel.XlReferenceStyle.xlA1)) { //String value of cell temp = currentFind.Value2.ToString(); break; } currentFind = excelCell.FindNext(currentFind); } //Find adjacent cell value here? holdInformation.Add(temp); } catch { Console.WriteLine("Couldn't get customer name"); } 

我认为偏移函数是你正在寻找的。 在你的代码上,你可以添加一行代码:

 firstFind.Offset[0, 1]; 

“0”表示您将目标行设置为“1”,表示您要将excel范围右侧的第一列作为目标