Excel自动化:Range.Find

我想在我的C#程序中实现这个方法。 但是我无法像在一行那样填写适当的参数

long FirstRow = myWorksheet.Cells.Find( What:="*", After:=Range("IV65536"), LookIn:=xlValues, LookAt:= xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row 

这里是Range.Find方法的文档。

 Range Find( [In] object What, [In, Optional] object After, [In, Optional] object LookIn, [In, Optional] object LookAt, [In, Optional] object SearchOrder, [In, Optional] XlSearchDirection SearchDirection, [In, Optional] object MatchCase, [In, Optional] object MatchByte, [In, Optional] object SearchFormat ); 

所以基本上我不知道如何制作适当的参数对象。

更新 Excel.Range范围;

  object What = "*"; object After = xlWorkSheet.get_Range("A1", "IV65536"); object LookIn = "xlValues"; object LookAt = "xlPart"; object SearchOrder = "xlByRows"; Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext; object MatchCase = System.Reflection.Missing.Value; object MatchByte = System.Reflection.Missing.Value; object SearchFormat = System.Reflection.Missing.Value; range = xlWorkSheet.Cells.Find( What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat ); 

给出“COMException被未处理:types不匹配(从HRESULTexception:0x80020005(DISP_E_TYPEMISMATCH))”

更新#2这是迄今为止的方法。 唯一缺less的是设置和返回范围。

  public void RealUsedRange() { int FirstRow = xlWorkSheet.Cells.Find( "*", xlWorkSheet.get_Range("IV65536", misValue), Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value ).Row; int FirstColumn = xlWorkSheet.Cells.Find( "*", xlWorkSheet.get_Range("IV65536", misValue), Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value ).Column; int LastRow = xlWorkSheet.Cells.Find( "*", xlWorkSheet.get_Range("IV65536", misValue), Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value ).Row; int LastColumn = xlWorkSheet.Cells.Find( "*", xlWorkSheet.get_Range("IV65536", misValue), Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value ).Column; } 

没有testing,但这给你的一般想法:

 long firstRow = myWorkSheet.Cells.Find( "*", /* What */ Range("IV65536"), /* After */ Excel.XlFindLookIn.xlValues, /* LookIn */ Excel.XlLookAt.xlPart, /* LookAt */ Excel.XlSearchOrder.xlByRows, /* SearchOrder */ Excel.XlSearchDirection.xlNext, /* SearchDirection */ Type.Missing, /* MatchCase */ Type.Missing, /* MatchByte */ Type.Missing /* SearchFormat */ ).Row; 

由于不能使用VB.NET的可选参数语法(没有C#v4),因此需要按顺序提供所有参数。 提供null可能适用于缺less参数,但我很确定Type.Missing是正确的填充。 除此之外,它只是像你所期望的那样调用它。

这里有一些完整的C#示例:

  • 如何:在工作表范围中search文本

你的下一个问题是LookInLookAtSearchOrder参数。 他们不应该是一个string,而是他们类似于SearchDirection参数:

 object What = "*"; object After = xlWorkSheet.get_Range("A1", "IV65536"); object LookIn = Excel.XlFindLookIn.xlValues; object LookAt = Excel.XlLookAt.xlPart; object SearchOrder = Excel.XlSearchOrder.xlByRows; Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext; object MatchCase = System.Reflection.Missing.Value; object MatchByte = System.Reflection.Missing.Value; object SearchFormat = System.Reflection.Missing.Value; range = xlWorkSheet.Cells.Find( What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat );