简体   繁体   English

如何使用Excel Interop获取已过滤行的范围?

[英]How can I get the Range of filtered rows using Excel Interop?

I'm using Excel Interop assemblies for my project, if I want to use auto filter with then thats possible using 我正在为我的项目使用Excel Interop程序集,如果我想使用自动过滤器那么可能使用

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. 过滤范围后,您可以通过使用Range.SpecialCells方法访问传递过滤条件的单元格,传入值为“Excel.XlCellType.xlCellTypeVisible”以获取可见单元格。

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. 从那里你可以通过'Range.Cells'集合访问可见范围内的每个单元格,或访问每一行,首先通过'Range.Areas'集合访问区域,然后迭代'Rows'中的每一行每个区域的集合。 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 麦克风

I used as mentioned below, similar to what Mike told, 我使用如下所述,类似于Mike所说的,

foreach (Excel.Range area in visibleCells.Areas)
{
 foreach(Excel.Range row in area.Rows)
 {
 int index = row.Row; // now index is the present Row index within the range
 string test = Mysheet.Cells[index,4].Values //// Mysheet is my present working sheet. After this test will contain the values pointing to the values.cells[index,4]
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM