How to set multiple filters in closedXML

我的梦境 提交于 2021-01-29 09:47:12

问题


There are two unanswered questions out there on this: Applying multiple filters in ClosedXML (SetAutoFilter) and ClosedXML Excel filter rows by values in multiple columns This code will leave a single filter on the last range:

var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("AutoFilter");
            ws.Cell("A1").Value = "Names";
            ws.Cell("A2").Value = "John";
            ws.Cell("A3").Value = "Hank";
            ws.Cell("A4").Value = "Dagny";

            var r1 = ws.Range("A1", "A4");
            r1.SetAutoFilter();

            ws.Cell("B1").Value = "Names";
            ws.Cell("B2").Value = "John";
            ws.Cell("B3").Value = "Hank";
            ws.Cell("B4").Value = "Dagny";
            var r2 = ws.Range("B1", "B4");
            r2.SetAutoFilter();

            wb.SaveAs(filterTest);

See the image: only one filter in excel file

How can I add multiple filters?


回答1:


There is a misunderstanding of filters here! One filter per page is the rule, code below produces the desired result.

var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("AutoFilter");
            ws.Cell("A1").Value = "Names";
            ws.Cell("A2").Value = "John";
            ws.Cell("A3").Value = "Hank";
            ws.Cell("A4").Value = "Dagny";


            ws.Cell("B1").Value = "id";
            ws.Cell("B2").Value = "1";
            ws.Cell("B3").Value = "2";
            ws.Cell("B4").Value = "3";
            var r2 = ws.Range("A1", "B4");
            r2.SetAutoFilter();

            wb.SaveAs(filtertest);

result



来源:https://stackoverflow.com/questions/60323701/how-to-set-multiple-filters-in-closedxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!