excel.Range.Find返回错误值C#

问题描述:

我想写一个应用程序遍历以分号分隔的内容的文本文件,并在另一个Excel文件的特定列中搜索每个值。excel.Range.Find返回错误值C#

我可以打开Excel中的文本文件,并以正确的方式打开(我使应用程序可见并检查它是否格式正确)。现在,如果我遍历文本表并使用Excel.Range.Find方法,它总是返回错误的行。所以应该有与工作簿或表或没有任何问题

Microsoft.Office.Interop.Excel.Range current_find = usedRange_2.Cells[3].Find((String)(row.Cells[7]).Value2 
Microsoft.Office.Interop.Excel.Range first_find = null 

//for each row in tableOftextFile 
if (first_find == null && current_find!=null) 
    { 
      first_find = current_find; 
    } 
do 
    { 
    if (((String)current_find.Cells[3].Value2).Contains((String)row.Cells[7].Value2)) 
     { 
      //this part is never reached somehow. 
      break; 
     } 
    else 
    { 
     current_find = usedRange_2.FindNext(current_find); 
    } 
    } while (current_find!=first_find && current_find!=null); 

一切都在我的应用程序的工作就好了。

的查找操作必须找到的东西,因为该值存在,我可以在Excel中的应用程序手动搜索他们,并得到正确的结果,但例如,如果我搜索的值“40”,并将结果返回到current_findcurrent_find.Cells[3].Value2是“42”或类似的东西,但不是我预期的结果。

如果有人可以请给我一个提示,甚至是一个很好的解决方案。谢谢:)

对不起,我发现我的错误,如果别人不一样,这里就是答案:

我认为Excel.Find方法返回的行与比赛,但它似乎返回细胞与比赛。所以我将current_find.Cells[3].Value2更正为current_find.Value2,我很高兴。