如何使用Excel Interop获得过滤行的范围? [英] How can I get the Range of filtered rows using Excel Interop?

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

问题描述

我正在为我的项目使用Excel Interop程序集,
如果我想使用自动过滤器,那么可能使用

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 ??

任何人都有想法?

推荐答案

一旦过滤了范围,您可以通过使用 Range.SpecialCells 方法,传入一个值 Excel.XlCellType.xlCellTypeVisible'以获取可见的单元格。

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)

从那里你可以通过Range.Cells集合访问每个可见范围的单元格,或访问每一行,首先通过Range.Areas集合访问这些区域,然后迭代每个区域的Rows集合中的每一行。例如:

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.
    }
}

希望这有帮助!

Mike

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

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