繁体   English   中英

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

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

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

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

但是如何获得过滤后的行?

谁能有想法?

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

根据上面的示例代码,访问可见单元格应如下所示:

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

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

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

希望这可以帮助!

麦克风

我使用如下所述,类似于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