C# 在C中计算已筛选excel范围的行#
我一直在一个c#项目中工作,我试图完成的是计算c#中过滤范围的可见行 首先,我创建一个要过滤的范围C# 在C中计算已筛选excel范围的行#,c#,.net,excel,interop,C#,.net,Excel,Interop,我一直在一个c#项目中工作,我试图完成的是计算c#中过滤范围的可见行 首先,我创建一个要过滤的范围 Range usedRange = sheet.Range[sheet.Cells[7, 1], sheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)]; 然后,我应用2个过滤器: usedRange.AutoFilter(20, "<>RAW", XlAutoFilterOperator.xlFilterValu
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);
如果我在范围内循环,我可以得到计数。但是,由于数据量很大,这可能会大大降低处理速度:
//this is not effective
foreach (Range area in argFiltered.Areas) {
foreach (Range areaRow in area) {
count++;
}
}
我的问题是:有没有办法“直接”获取可见的过滤行数?
谢谢大家!
注:
如果我选择范围,它将选择应该显示在计数!中的确切行数
argFiltered.Select();
不理想,但这会更好一些:
foreach (Range area in argFiltered.Areas) {
count += area.Rows.Count;
}
你能负担得起在一些未使用的单元格中放入一个计算行数的公式吗?如果使用代码为3的函数,则不会计算由筛选器隐藏的行数。将公式放入单元格并读取其值,例如
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();
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();