【问题标题】:How can I get the Range of filtered rows using Excel Interop?如何使用 Excel 互操作获取筛选行的范围?
【发布时间】:2010-12-27 08:28:27
【问题描述】:

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

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

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

谁能有想法??

【问题讨论】:

    标签: c# excel automation vsto


    【解决方案1】:

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

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

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

    从那里,您可以通过“Range.Cells”集合访问可见范围内的每个单元格,或者访问每一行,首先通过“Range.Areas”集合访问区域,然后在每个区域的“行”集合。例如:

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

    希望这会有所帮助!

    迈克

    【讨论】:

    • 投反对票的人可以解释一下吗?这是一个干净的解决方案,它确实适用于原始海报。这里有什么不适合你的吗?
    • 同意,在对任何答案投反对票时,应该发表评论, - 所以至少任何用户都可以更新它或提供更好的解决方案:)
    • 我没有否决它,但它对我也不起作用。返回的范围仅到第一个隐藏行。它不会“跳过”任何间隙。
    • 嗨,我正在尝试获取 E 列可见单元格的值,但它只是循环遍历单元格的每个字符。你有什么想法? foreach (Excel.Range area in filterValue.Areas) { foreach (var value in area.Columns[5].Cell.Value)
    • 是否可以一次移动整个区域而不是逐行移动?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多