转载:从 .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
转载自: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# 开发人员不需要担心这个问题,因为他们在每个方法调用时都必须提供所有参数。
图 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# 开发人员则必须为每个参数赋予值。)
图 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); } |
相关文章推荐
- 转载:从 .NET 开发人员的角度理解 Excel 对象模型(Range)[续]
- 从 .NET 开发人员的角度理解 Excel 对象模型 (From MSDN)
- [转]从 .NET 开发人员的角度理解 Excel 对象模型
- 从 .NET 开发人员的角度理解 Excel 对象模型
- 从.NET开发人员的角度理解Excel对象模型
- 从 .NET 开发人员的角度理解 Excel 对象模型
- 从 .NET 开发人员的角度理解 Excel 对象模型
- 从.NET开发人员的角度理解Excel对象模型
- 从 .NET 开发人员的角度理解 Excel 对象模型
- 从开发人员的角度理解 Excel 对象模型 (二)
- 开发人员的角度理解 Excel 对象模型
- 从开发人员的角度理解 Excel 对象模型 (一)
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- 开篇:从开发人员的角度理解产品经理
- [转载].NET开发人员必知的八个网站
- .NET开发Silverlight程序:界面和对象模型
- 使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型