转载:从 .NET 开发人员的角度理解 Excel 对象模型(Range)[续]
2008-05-14 17:32 Koy 阅读(872) 评论(0) 收藏 举报转载自:http://hi.baidu.com/daijun2007/blog/item/e1e08bfdb998b540d6887daa.html
|
在范围中查找 Range 类的 Find 方法允许您在范围内搜索文本。这个灵活的方法模仿 Excel 中的查找和替换对话框的行为,如图 26 所示 - 实际上,这个方法直接和这个对话框交互。也就是说,Range.Find 方法或者使用您传递给它的参数来决定它的搜索行为,或者如果您没有传递参数,它就使用其在查找和替换对话框中的值来进行查找。表 4 列出了 Range.Find 方法的参数,除了第一个参数外,其他所有参数都是可选的。
图 26. 在这个对话框上的选择会影响 Find 方法的行为。
警告因为 Range.Find 的几乎所有参数都是可选的,同时因为用户可能通过“查找和替换”对话框改变值,所以您要确保真正将所有值传给了 Find 方法,除非您想将用户的选择也考虑在内。当然,C# 开发人员不需要担心这个问题,因为他们在每个方法调用时都必须提供所有参数。
以下示例来自示例工作簿,它搜索一个范围(名称为“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);
}
}
这段代码采取这些步骤来实现其目的:
单击示例工作表的 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# 开发人员则必须为每个参数赋予值。)
提示当调用像这样的方法时,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);
}
|
.gif)
.gif)
.gif)
浙公网安备 33010602011771号