我想在我的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;
}
是否使用框架4.0?你有什麼樣的錯誤使用你的語法? – Marco 2011-05-24 06:19:22
我正在使用Framework 3.5。我知道4.0中的[動態類型系統](http://www.devx.com/dotnet/Article/42590),如果這是你所指的。目前我沒有得到任何錯誤,因爲我不知道完整的線路還沒有執行。對於那些未使用的參數,當然應該有一個System.Reflection.Missing.Value對象。 – 2011-05-24 06:26:57