C#-Excel自动化:Range.Find
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-Excel自动化:Range.Find,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3196字,纯文字阅读大概需要5分钟。
内容图文
我想在我的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.范围
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;
}
解决方法:
未经测试,但是这给了您大致的想法:
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#示例:
> How to: Search for Text in Worksheet Ranges
内容总结
以上是互联网集市为您收集整理的C#-Excel自动化:Range.Find全部内容,希望文章能够帮你解决C#-Excel自动化:Range.Find所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。