问题
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