Sort PivotTable data and row fields not working

I’m using Aspose.Cells to create a PivotTable and need the data and row filters to be sorted. Despite using the code below, the generated Excel file doesn’t apply the sorting. Could anyone provide guidance on how to resolve this?

// Draging the field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 3);
// Draging the field to the column area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 0);

pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 4);

pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Page, 1);

// Access the Page field (Region) and set the sort order
PivotField pageField = pivotTable.PageFields[0];
pageField.IsAscendSort = true; // Sort in ascending order

// Access the Row field (Product) and set the sort order
PivotField rowField = pivotTable.RowFields[0];
rowField.IsAscendSort = true; // Sort in ascending order

image.png (47.1 KB)

MyExported0815-sample2filters.zip (3.4 MB)

@lfan,

Thanks for the template XLSX file and screenshot.

Please try to add a line to your code segment to do your desired sorting for the pivot row field. I have tested with it using your template XLSX file and it works fine.

// Access the Row field (Product) and set the sort order
PivotField rowField = pivotTable.RowFields[0];
rowField.IsAutoSort = true;
rowField.IsAscendSort = true; // Sort in ascending order

@amjad.sahi,

It worked like a charm! The problem resolved. Than you so much!

@lfan,

You’re welcome. If you have any additional questions or comments, please feel free to reach out to us again.