簡體   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