-
Notifications
You must be signed in to change notification settings - Fork 297
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.
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 Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles