Pivot table sorting - DON-PHAM/EPPlus GitHub Wiki

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 Sample 18-.NET Framework or Sample 18-.NET Core