我怎样才能获得使用Excel互操作的过滤行的范围? [英] How can I get the Range of filtered rows using Excel Interop?

查看:137
本文介绍了我怎样才能获得使用Excel互操作的过滤行的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Excel互操作程序集为我的项目,
如果我想使用自动过滤器然后用那可能

<$p$p><$c$c>sheet.UsedRange.AutoFilter(1,SheetNames[1],Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd,oMissing,false)

但我怎样才能得到过滤后的行??

任何人都可以有想法??


解决方案

一旦你过滤的范围内,您可以访问通过使用的<一个通过筛选条件的单元格href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.specialcells(office.11).aspx\">Range.SpecialCells方法,传递一个有价值Excel.XlCellType.xlCellTypeVisible,以获得可见的单元格。

根据你的榜样,code

,上面访问可见单元应该是这个样子:

  Excel.Range visibleCells = sheet.UsedRange.SpecialCells(
                               Excel.XlCellType.xlCellTypeVisible,
                               Type.Missing)

从那里可以访问在可见光范围内的每个小区,通过'Range.Cells'集合,或访问各个行,通过首先通过'Range.Areas'集合访问区域,然后迭代内的每个行行收集各个区域。例如:

 的foreach(在visibleCells.Areas Excel.Range区)
{
    的foreach(在area.Rows Excel.Range行)
    {
        //处理每一未过滤,可见行这里。
    }
}

希望这有助于!

迈克

I'm using Excel Interop assemblies for my project, if I want to use auto filter with then thats possible using

sheet.UsedRange.AutoFilter(1,SheetNames[1],Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd,oMissing,false)

but how can I get the filtered rows ??

can anyone have idea??

解决方案

Once you filtered the range, you can access the cells that pass the filter criteria by making use of the Range.SpecialCells method, passing in a valued of 'Excel.XlCellType.xlCellTypeVisible' in order to get the visible cells.

Based on your example code, above, accessing the visible cells should look something like this:

Excel.Range visibleCells = sheet.UsedRange.SpecialCells(
                               Excel.XlCellType.xlCellTypeVisible, 
                               Type.Missing)

From there you can either access each cell in the visible range, via the 'Range.Cells' collection, or access each row, by first accessing the areas via the 'Range.Areas' collection and then iterating each row within the 'Rows' collection for each area. For example:

foreach (Excel.Range area in visibleCells.Areas)
{
    foreach (Excel.Range row in area.Rows)
    {
        // Process each un-filtered, visible row here.
    }
}

Hope this helps!

Mike

这篇关于我怎样才能获得使用Excel互操作的过滤行的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆