2017-01-19 26 views
2

我一直在C#項目中工作,我正在努力實現的是計算C#中過濾範圍的可見行。在C中計算過濾excel範圍的行#

首先,我創建了一個我要過濾的範圍。

Range usedRange = sheet.Range[sheet.Cells[7, 1], sheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)]; 

然後,我申請2個過濾器:

usedRange.AutoFilter(20, "<>RAW", XlAutoFilterOperator.xlFilterValues, "<>AV", true); 
    usedRange.AutoFilter(2, "ARG", XlAutoFilterOperator.xlFilterValues, Type.Missing, true); 

而在去年,我創建另一個範圍內,使用 「XlCellType.xlCellTypeVisible」,並嘗試計算行數,但伯爵返回1

argFiltered = usedRange.SpecialCells(XlCellType.xlCellTypeVisible); 
    Console.WriteLine("Number of rows: "+ argFiltered.Rows.Count) // the count returns 1. 

如果我遍歷範圍我可以得到計數。但隨着數據的數量較大這樣能減緩的過程中有很多:

//this is not effective 
foreach (Range area in argFiltered.Areas) { 
    foreach (Range areaRow in area) { 
     count++; 
    } 
    } 

我的問題是:有沒有辦法讓可見光過濾行「直接」是多少?

謝謝大家!

注意: 如果我選擇範圍,它會選擇計數中應顯示的確切行數!

argFiltered.Select(); 

回答

0

不理想,但這個會有所好轉:

foreach (Range area in argFiltered.Areas) { 
    count += area.Rows.Count; 
} 
0

你能負擔得起把公式中的罪名行一些未使用的電池?如果對代碼3使用Subtotal函數,它將不計算過濾器隱藏的行數。在單元格中輸入公式並讀取其值,例如

sheet.Cells[1, 255].Formula = "=subtotal(3, A1:A16383)"; //Count all rows that have a non-blank column A and aren't hidden 
var count = sheet.Cells[1, 255].Value; 
sheet.Cells[1, 255].ClearContents();