Skip to content

Pivot table filters and slicers

Jan Källman edited this page Oct 1, 2020 · 8 revisions

Pivot table filters are filters that are applied to Pivot table fields. Pivot table slicers are drawing objects that map to a Pivot table fields value filter. These two features are supported from EPPlus 5.4.

Pivot table filters

EPPlus supports the following filters for Pivot tables:

  • Item filters - Filters on individual items in row/column or page fields.
  • Caption filters (label filters) - Filters for text on row and column fields.
  • Date, numeric and string filters - Filters using various operators such as Equals, NotBetween, GreaterThan, etc.
  • Top 10 filters - Filter using top or bottom criterias for percent, count or value.
  • Dynamic filters - Filter using various date- and average criterias.

The following code is from Sample 13 (link below)

ExcelRangeBase range = await LoadFromDatabase(connectionString, ws);

var tbl = ws.Tables.Add(range, "ptFilter");
tbl.TableStyle = OfficeOpenXml.Table.TableStyles.Medium23;

var pt1=ws.PivotTables.Add(ws.Cells["J1"], tbl, "PivotTable1");
var rowField = pt1.RowFields.Add(pt1.Fields["CompanyName"]);
var dataField = pt1.DataFields.Add(pt1.Fields["OrderValue"]);

//First deselect a company in the items list. To do so we first need to refresh the items from the range.
rowField.Items.Refresh();  //Refresh the items from the range.
rowField.Items.GetByValue("Sporer, Mertz and Jaskolski").Hidden=true;
//Add a caption filter on Company Name between A and D
rowField.Filters.AddCaptionFilter(ePivotTableCaptionFilterType.CaptionBetween, "A", "D");
//Add a value filter where OrderValue >= 100
rowField.Filters.AddValueFilter(ePivotTableValueFilterType.ValueGreaterThanOrEqual, dataField, 100);

//Add a second pivot table with some different filters.
var pt2 = ws.PivotTables.Add(ws.Cells["M1"], tbl, "PivotTable2");
var rowField1 = pt2.RowFields.Add(pt2.Fields["Currency"]);
var rowField2 = pt2.RowFields.Add(pt2.Fields["OrderDate"]);
rowField2.Format = "yyyy-MM-dd";
var dataField1 = pt2.DataFields.Add(pt2.Fields["OrderValue"]);
var dataField2 = pt2.DataFields.Add(pt2.Fields["OrderId"]);
dataField2.Function = DataFieldFunctions.CountNums;

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally