简体   繁体   English

使用C#的高级过滤器excel

[英]advanced filter excel with c#

I need to do a filter in a excel sheet, I would like to know if is possible do a filter like this 我需要在Excel工作表中做一个过滤器,我想知道是否有可能做这样的过滤器

 List<string> listFilter = new List<string>();
            listFilter.Add("3");
            listFilter.Add("4");

            object _missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Range oRng1 = xlWorkSheet.Range["A1", "A1048576"];
            oRng1.AutoFilter(1, listFilter, Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd, _missing, true);
            oRng1.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

I know that this code doesn't work but I would like to do a dinamic list in the filter parameter. 我知道这段代码行不通,但是我想在filter参数中做一个动态列表。 Could someone tell me how can I do this? 有人可以告诉我该怎么做吗?

I'm not sure about passing lists, but you can definitely pass arrays: 我不确定要传递列表,但是您绝对可以传递数组:

string[] listfilter = new string[] { "2", "3", "4" };
xlWorksheet.get_Range("A1", "B50").AutoFilter(1, listfilter, Excel.XlAutoFilterOperator.xlFilterValues,
    Missing.Value, true);

You can find the different members of the XlAutoFilterOperator here . 您可以在此处找到XlAutoFilterOperator的不同成员。

It might also be an idea to try to find the last-used row in the sheet rather than setting the filter for the entire column as that might slow things down a little: 尝试在工作表中查找最后使用的行而不是为整个列设置过滤器也可能是一个主意,因为这可能会使速度降低一些:

int lastRow = xlWorksheet.Range["A:A"].Find("*", Missing.Value, Missing.Value, Missing.Value,
    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, Missing.Value,
    Missing.Value).Row;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM