问题
I have a Excel file with several thousand rows and columns up to "BP". I need to filter all of these rows by specific values in columns C and BP.
I tested the filter functionality in ClosedXML as per the code below.
When I apply a filter to one column all works well and the data is saved in the new file.
When I try to apply two filters, the last one executed is the one that is applied.
I have tried to use the worksheet as a Range/Table, same filtering problem.
I eventually created the "rows" expression, that works, but the 1st row (header) is filtered out.
public static void Filter(string source, string newFile)
{
using (var workbook = new XLWorkbook(source))
{
IXLWorksheet worksheet = workbook.Worksheet(1);
int salesFoundCell = worksheet.FirstRow().Cells().First(c => c.Value.ToString() == "Sales Order Description").Address.ColumnNumber;
int revenueFoundCell = worksheet.FirstRow().Cells().First(c => c.Value.ToString() == "Revenue recognition date").Address.ColumnNumber;
//worksheet.RangeUsed().SetAutoFilter().Column(salesFoundCell).EqualTo("Equipment Sale");
//worksheet.RangeUsed().SetAutoFilter().Column(revenueFoundCell).EqualTo("00.00.0000");
//var rows = worksheet.RowsUsed().Where(r => r.CellsUsed().Any(c => c.GetString().Contains("Equipment Sale")) &&
// r.CellsUsed().Any(c => c.GetString().Contains("00.00.0000")));
Console.WriteLine(rows.Count());
//workbook.SaveAs(newFile);
}
}
I also tried the method posted on the ClosedXML wiki, where you save the worksheet as a MemoryStream, reapply the filter and then save it to a new file.
This is the short version:
public void Create(string filePath)
{
var wb = new XLWorkbook();
IXLWorksheet ws;
#region Multi Column
String multiColumn = "Multi Column";
ws = wb.Worksheets.Add(multiColumn);
// Add filters
ws.RangeUsed().SetAutoFilter().Column(2).EqualTo(3).Or.GreaterThan(4);
ws.RangeUsed().SetAutoFilter().Column(3).Between("B", "D");
// Sort the filtered list
ws.AutoFilter.Sort(3);
#endregion
using (var ms = new MemoryStream())
{
wb.SaveAs(ms);
var workbook = new XLWorkbook(ms);
#region Multi Column
workbook.Worksheet(multiColumn).AutoFilter.Column(3).EqualTo("E");
workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
#endregion
workbook.SaveAs(filePath);
ms.Close();
}
}
I went through several iterations of the below two expressions:
worksheet.RangeUsed().SetAutoFilter().Column(salesFoundCell).EqualTo("Equipment Sale");
worksheet.RangeUsed().SetAutoFilter().Column(revenueFoundCell).EqualTo("00.00.0000");
I tried filtering directly on the columns, as a range, as a table, trying to hide the rows that did not have the required values.
All of it either filters based on one column or not at all.
The "expression.AddFilter(some value).AddFilter(some other value);" does not help as I am not trying to add multiple filters on the same column
The "And/Or" functionality does the same, multiple filters on the same column.
Has anyone managed to filter based on values in multiple columns?
Any advice is much appreciated.
回答1:
Try the below sorting method found here
myRange.SortColumns.Add(firstColumnNumber, XLSortOrder.Ascending);
myRange.SortColumns.Add(secondColumnNumber, XLSortOrder.Ascending);
myRange.Sort();
回答2:
Here's my answer. I struggled with the same problem for a while.
The key is the sort, which has to be done after you define the filters.
var excelTable = TableRange.CreateTable();
excelTable.AutoFilter.Column(26).AddFilter("Filter 1");
excelTable.AutoFilter.Column(26).AddFilter("Filter 2");
excelTable.AutoFilter.Sort(1, XLSortOrder.Ascending);
来源:https://stackoverflow.com/questions/43324643/closedxml-excel-filter-rows-by-values-in-multiple-columns