2011-05-24 69 views
3

我想在我的c#程序中實現this方法。但是我無法在適當的參數填充線像Excel自動化:Range.Find

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

Here is the documentation for the Range.Find method.

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 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是未處理的:類型不匹配(來自HRESULT異常:0x80020005(DISP_E_TYPEMISMATCH))」

更新#2 這裏是迄今爲止方法。唯一缺少的是設置和返回範圍。

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; 
    } 
+1

是否使用框架4.0?你有什麼樣的錯誤使用你的語法? – Marco 2011-05-24 06:19:22

+0

我正在使用Framework 3.5。我知道4.0中的[動態類型系統](http://www.devx.com/dotnet/Article/42590),如果這是你所指的。目前我沒有得到任何錯誤,因爲我不知道完整的線路還沒有執行。對於那些未使用的參數,當然應該有一個System.Reflection.Missing.Value對象。 – 2011-05-24 06:26:57

回答

3

沒有測試,但是這給你的總體思路:

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可能適用於缺少參數,但我很確定Type.Missing是正確的填充。除此之外,它只是像你期望的那樣調用它。

這裏有一些完整的C#示例:

+0

謝謝。我可能會更近一步,但我仍然有不知道如何正確設置參數的問題。任何幫助表示讚賞。我已經用新代碼更新了我的第一篇文章。 – 2011-05-24 07:12:30

+0

我已經移植了這些參數並添加了一個包含所有血腥細節的鏈接。 – 2011-05-24 07:22:29

+0

謝謝。這是我需要的提示:-) – 2011-05-24 07:25:08

1

你的下一個問題是看着注視SearchOrder參數。他們不應該是一個字符串,而他們是類似於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 
);