我想在我的c#程序中实现this方法。但是我在填写像
这样的行中的相应参数时遇到了麻烦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范围;
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;
}
答案 0 :(得分: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;
由于在没有C#v4的情况下无法使用VB.NET的可选参数语法,因此需要按顺序提供所有参数。提供null
可能适用于缺失的args,但我非常确定Type.Missing
是正确的填充程序。除此之外,它只是像你期望的那样调用它。
以下是一些完整的C#示例:
答案 1 :(得分:1)
您的下一个问题是 LookIn , LookAt 和 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
);