Skip to content

Pivot table sorting

Mats Alm edited this page Nov 6, 2023 · 6 revisions

You can sort pivot tables by setting the PivotTable Field - Sort property.

rf.Sort = eSortType.Ascending;

This applies to column and row fields. You can also sort on data fields by using the SetAutoSort method.

rowField.SetAutoSort(df, eSortType.Descending);

This method sets the AutoSort property that contains the pivot area to set for the pivot table field sorting.

If you have multiple row and column fields you can set the auto sort by using pivot areas. This sample sorts by the column field value "Hardware".

var rowField = p1.RowFields.Add(p1.Fields[0]);
var columnField = p1.ColumnFields.Add(p1.Fields[1]);
var dataField = p1.DataFields.Add(p1.Fields[3]);
rf.SetAutoSort(df, eSortType.Descending);
var reference = rf.AutoSort.Conditions.Fields.Add(cf);
cf.Items.Refresh(); //You must refresh the items to fill the items collection with the correct values.
reference.Items.AddByValue("Hardware");

See also

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally