簡體   English   中英

C#在多個值上過濾excel列

[英]C# Filter excel columns on more than one value

我正在使用C#在基於Windows窗體的應用程序中將數據表導出到Excel。 FilterList具有以下值

string[] FilterList = new string[] {"Red", "Blue"};

但是我只得到被“藍色”過濾的值。 下面是我在其中一列上應用過濾器的部分代碼。我要過濾的列中有7個不同的值,我只想從中選擇2個。

Microsoft.Office.Interop.Excel.Application app = new
Microsoft.Office.Interop.Excel.Application();   
        app.Visible = false;

        Workbook wb = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);   
        Worksheet ws = (Worksheet)wb.ActiveSheet;

       // Some business logic to fill the excel.............

        Range firstRow = (Excel.Range)ws.Rows[1];
        firstRow.Activate();
        firstRow.Select();
        firstRow.AutoFilter(5, FilterList.Count > 0 ? FilterList :
        Type.Missing,Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);

我在這里做錯了什么,有什么幫助

好了,你去:

Range的Autofilter方法的第三個參數接受XlAutoFilterOperator,我將其更改為xlFilterValues而不是xlAnd因為我使用的是單個條件對象,但具有多個條件。 下面是我為使過濾器選擇2個值所做的代碼更改。

Range.AutoFilter(5, FilterList.Count > 0 ? FilterList.ToArray() : Type.Missing,
Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);

資料來源: SocialMSDN

希望它對其他SO用戶有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM