您的位置:首页 > 其它

转载:从 .NET 开发人员的角度理解 Excel 对象模型(Range)[续]

2008-05-14 17:31 645 查看
从 .NET 开发人员的角度理解 Excel 对象模型(Range)[续]

转载自:http://hi.baidu.com/daijun2007/blog/item/e1e08bfdb998b540d6887daa.html

2008-04-24 09:58
在范围中查找

Range 类的 Find 方法允许您在范围内搜索文本。这个灵活的方法模仿 Excel 中的查找和替换对话框的行为,如图 26 所示 - 实际上,这个方法直接和这个对话框交互。也就是说,Range.Find 方法或者使用您传递给它的参数来决定它的搜索行为,或者如果您没有传递参数,它就使用其在查找和替换对话框中的值来进行查找。 4 列出了 Range.Find 方法的参数,除了第一个参数外,其他所有参数都是可选的。



26. 在这个对话框上的选择会影响 Find 方法的行为。
警告因为 Range.Find 的几乎所有参数都是可选的,同时因为用户可能通过“查找和替换”对话框改变值,所以您要确保真正将所有值传给了 Find 方法,除非您想将用户的选择也考虑在内。当然,C# 开发人员不需要担心这个问题,因为他们在每个方法调用时都必须提供所有参数。




参数

类型

说明

What(必需的)

对象

要查找的数据;可以是一个字符串或者任何 Excel 数据类型。

After

范围

您想从这个范围的后面开始搜索(在搜索中,不包括这个单元格);如果不指定单元格,则从范围的左上角开始搜索。

LookIn

XlFindLookin(xlValue、xlComments、xlFormulas)

要搜索的信息类型;不能用 Or 运算符组合查询。

LookAt

XlLookAt(xlWhole、xlPart)

确定搜索匹配所有单元格,还是部分单元格。

SearchOrder

XlSearchOrder(xlByRows、xlByColumns)

决定搜索顺序;xlByRows(默认值)将横向搜索,然后纵向搜索;xlByColumns 将纵向搜索,然后横向搜索。

SearchDirection

XlSearchDirection(xlNext、xlPrevious)

确定搜索的方向;默认值是 xlNext。

MatchCase

布尔值

确定搜索是否区分大小写。

MatchByte

布尔值

确定是否双字节字符只和双字节匹配 (True) 或者也可以和单字节字符匹配 (False);只有当您安装了对双字节支持时才适用。

以下示例来自示例工作簿,它搜索一个范围(名称为“Fruits”),并更改含有单词“apples”的单元格的字体( 27 显示了搜索结果)。这个过程也使用了 FindNext 方法,它使用前面设好的搜索设置重复搜索。(Range.FindPrevious 方法和 Range.FindNext 方法的使用几乎一样,但这个示例没用到。)您要指定在哪个单元格后搜索,而剩下的就由 FindNext 方法处理。



27. 包含单词“apples”的单元格的搜索结果
提示 FindNext(和 FindPrevious)方法一旦搜索到范围的末端,就会重新回到搜索范围的开始位置。要确保搜索不会成为无限循环,永远不休,您需要在代码中设定。示例过程演示了处理这种情况的一种方法。如果您想完全避免这种无限循环,或者您想进行一个比 Find/FindNext/FindPrevious 方法更加复杂的搜索,那么您也可以使用一个 For Each 循环在一个范围内对所有单元格进行循环查找。

单击示例工作簿的 Range Class 工作表中的 Find 链接来运行以下过程:

' Visual Basic
Private Sub DemoFind()
Dim rng As Excel.Range = ThisApplication.Range("Fruits")
Dim rngFound As Excel.Range
' Keep track of the first range you find.
Dim rngFoundFirst As Excel.Range
' You should specify all these parameters
' every time you call this method, since they
' can be overriden in the user interface.
rngFound = rng.Find( _
"apples", , _
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
False)
While Not rngFound Is Nothing
If rngFoundFirst Is Nothing Then
rngFoundFirst = rngFound
ElseIf rngFound.Address = rngFoundFirst.Address Then
Exit While
End If
With rngFound.Font
.Color = ColorTranslator.ToOle(Color.Red)
.Bold = True
End With
rngFound = rng.FindNext(rngFound)
End While
End Sub
// C#
private void DemoFind()
{
Excel.Range rng = ThisApplication.
get_Range("Fruits", Type.Missing);
Excel.Range rngFound;
// Keep track of the first range you find.
Excel.Range rngFoundFirst = null;
// You should specify all these parameters
// every time you call this method, since they
// can be overriden in the user interface.
rngFound = rng.Find("apples", Type.Missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
false, Type.Missing, Type.Missing);
while (rngFound != null)
{
if (rngFoundFirst == null )
{
rngFoundFirst = rngFound;
}
else if (GetAddress(rngFound) == GetAddress(rngFoundFirst))
{
break;
}
rngFound.Font.Color = ColorTranslator.ToOle(Color.Red);
rngFound.Font.Bold = true;
rngFound = rng.FindNext(rngFound);
}
}

这段代码采取这些步骤来实现其目的:

声明 Excel.Range 变量来跟踪整个范围、第一个被发现的范围和当前发现的范围:

' Visual Basic
Dim rng As Excel.Range = ThisApplication.Range("Fruits")
Dim rngFound As Excel.Range
Dim rngFoundFirst As Excel.Range
// C#
Excel.Range rng = ThisApplication.
get_Range("Fruits", Type.Missing);
Excel.Range rngFound;
Excel.Range rngFoundFirst = null;


搜索第一个匹配值,指定所有的参数(要在以后搜索的单元格除外) — 默认情况下,搜索从范围左上角的单元格开始 — 然后在单元格值中搜索“apples”,匹配部分值,逐行向前搜索,并且不区分大小写:

' Visual Basic
rngFound = rng.Find( _
"apples", , _
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, _
False)
// C#
rngFound = rng.Find("apples", Type.Missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
false, Type.Missing, Type.Missing);


只要还能发现匹配值搜索就会继续下去:

' Visual Basic
While Not rngFound Is Nothing
' Code removed here...
End While
// C#
while (rngFound != null)
{
// Code removed here...
}


将第一个发现的范围 (rngFoundFirst) 和 Nothing 进行比较,如果代码只发现第一个匹配值,rngFoundFirst 就会为 Nothing,也只有在这种情况下它才会为 Nothing。在这种情况下,代码将找到的范围保存起来;否则,如果找到的范围地址和第一个找到的范围地址一致,代码会退出循环。

' Visual Basic
If rngFoundFirst Is Nothing Then
rngFoundFirst = rngFound
ElseIf rngFound.Address = rngFoundFirst.Address Then
Exit While
End If
// C#
if (rngFoundFirst == null )
{
rngFoundFirst = rngFound;
}
else if (GetAddress(rngFound) == GetAddress(rngFoundFirst))
{
break;
}


设置找到的范围的外观:

' Visual Basic
With rngFound.Font
.Color = ColorTranslator.ToOle(Color.Red)
.Bold = True
End With
// C#
rngFound.Font.Color = ColorTranslator.ToOle(Color.Red);
rngFound.Font.Bold = true;


执行另一次搜索:

' Visual Basic
rngFound = rng.FindNext(rngFound)
// C#
rngFound = rng.FindNext(rngFound);


单击示例工作表的 Reset Find 链接来运行这个简单的过程,开始运行时将会重新设置范围:

' Visual Basic
Private Sub ResetFind()
Dim rng As Excel.Range = ThisApplication.Range("Fruits")
With rng.Font
.Color = ColorTranslator.ToOle(Color.Black)
.Bold = False
End With
End Sub
// C#
private void ResetFind()
{
Excel.Range rng = ThisApplication.
get_Range("Fruits", Type.Missing);
rng.Font.Color = ColorTranslator.ToOle(Color.Black);
rng.Font.Bold = false;
}

提示 如果您想在一个范围内查找和替换,请使用 Range.Replace 方法。这个方法的使用类似于 Find 方法,但是可以让您指定要替换的值。 Replace 方法返回一个指示是否执行替换的 Boolean 值。即使只替换一个值,它也会返回 True。

在范围中对数据进行排序

就如通过 Excel 用户界面对一个范围内的数据进行排序一样,您也可以采用编程方式使用 Range.Sort 方法对数据进行排序。您指出要被排序的范围,要进行排序的至多三行或三列(可选),以及其他可选的参数,剩下的则由 Excel 来处理。 5 列出了 Sort 方法的所有参数。(Visual Basic .NET 开发人员很可能只会用到其中的一部分,而 C# 开发人员则必须为每个参数赋予值。)




参数

类型

说明

Key1

Object(String 或 Range)

首要排序字段,可以是一个范围名称 (String),或是一个 Range 对象,确定了要排序的值。

Order1

XlSortOrder(xlAscending、xlDescending)

为 Key1 中指定的值决定排序顺序。

Key2

Object(String 或 Range)

第二个排序字段,排序透视表时无法使用。

Type

Object

当对透视表进行排序时,指定对哪些元素排序;对一个普通范围则没有影响。

Order2

XlSortOrder

为在 Key2 中指定的值决定排序顺序。

Key3

Object(String 或 Range)

第三个排序字段,不能使用于透视表。

Order3

XlSortOrder

为在 Key3 中指定的值决定排序顺序。

Header

XlYesNoGuess(xlGuess、xlNo、xlYes)

指定第一行是否包含头信息,默认值为 xlNo;如果想让 Excel 自己去推测,就指定为 xlGuess。

OrderCustom

Integer

为自定义排序顺序列表指定一个基于 1 的索引;如果不指定这个参数,则使用默认排序顺序。图 28 显示了一种创建自定义排序顺序的技术。对于这个例子,将这个参数指定为 6 将基于“fruits”自定义顺序进行排序。

MatchCase

Boolean

设置成 True 就会进行区分大小写的排序,设置成 False 则进行不区分大小写的排序;不能用于透视表。

Orientation

XlSortOrientation (xlSortRows, xlSortColumns)

排序方向。

SortMethod

XlSortMethod(xlStroke、xlPinYin)

指定排序方法;不能适用于所有语言(当前值只适用于对汉字进行排序,而不适用于对其他语言排序)。

DataOption1

XlSortDataOption (xlSortTextAsNumbers, xlSortNormal)

指定如何对 Key1 中指定的范围进行文本排序;不能用于透视表排序。

DataOption2

XlSortDataOption

指定如何对 Key2 中指定的范围进行文本排序;不能用于透视表排序。

DataOption3

XlSortDataOption

指定如何对 Key3 中指定的范围进行文本排序;不能用于透视表排序。

提示当调用像这样的方法时,Visual Basic .NET 开发人员相对于 C# 开发人员来说,有着明显的优势。因为您不太可能会用到所有参数,Visual Basic .NET 开发人员能够使用命名的参数,只须指定他们需要的参数即可。而为了接受默认行为,C# 开发人员必须将所有不使用的参数传递 null 值。



图 28. 您可以创建自己的自定义排序列表,然后在代码中引用这些特定的排序顺序。
单击 Range Class 示例工作表中的 Sort 链接运行以下过程,它首先根据第一列中的数据来对“Fruits”范围排序,然后根据第二列中的数据排序:

' Visual Basic
Private Sub DemoSort()
Dim rng As Excel.Range = ThisApplication.Range("Fruits")
rng.Sort( _
Key1:=rng.Columns(1), Order1:=Excel.XlSortOrder.xlAscending, _
Key2:=rng.Columns(2), Order2:=Excel.XlSortOrder.xlAscending, _
Orientation:=Excel.XlSortOrientation.xlSortColumns, _
Header:=Excel.XlYesNoGuess.xlNo)
End Sub
// C#
private void DemoSort()
{
Excel.Range rng = ThisApplication.
get_Range("Fruits", Type.Missing);
rng.Sort(rng.Columns[1, Type.Missing],
Excel.XlSortOrder.xlAscending,
rng.Columns[2, Type.Missing],Type.Missing,
Excel.XlSortOrder.xlAscending,
Type.Missing, Excel.XlSortOrder.xlAscending,
Excel.XlYesNoGuess.xlNo, Type.Missing, Type.Missing,
Excel.XlSortOrientation.xlSortColumns,
Excel.XlSortMethod.xlPinYin,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal);
}

单击同一个工作表中的 Reset Sort 链接来运行以下过程,它根据自定义排序方法对第二列进行排序,如 28 所示:

' Visual Basic
Private Sub ResetSort()
Dim rng As Excel.Range = ThisApplication.Range("Fruits")
rng.Sort(rng.Columns(2), OrderCustom:=6, _
Orientation:=Excel.XlSortOrientation.xlSortColumns, _
Header:=Excel.XlYesNoGuess.xlNo)
End Sub
// C#
private void ResetSort()
{
Excel.Range rng = ThisApplication.
get_Range("Fruits", Type.Missing);
rng.Sort(rng.Columns[2, Type.Missing],
Excel.XlSortOrder.xlAscending,
Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending,
Type.Missing, Excel.XlSortOrder.xlAscending,
Excel.XlYesNoGuess.xlNo, 6, Type.Missing,
Excel.XlSortOrientation.xlSortColumns,
Excel.XlSortMethod.xlPinYin,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: