Hi Periasamy,
Thank you for contacting Aspose support.
You need to enable the AutoSort for the required PivotField and then specify the index on which you wish to sort the Grand Total. We have devised a generic solution for better elaboration of above concepts. Please check the following code snippet that creates a PivotTable from scratch and applies the descending sort on the Grand Total.
C#
var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
var cells = sheet.Cells;
var cell = cells[“A1”];
cell.PutValue(“Sport”);
cell = cells[“B1”];
cell.PutValue(“Quarter”);
cell = cells[“C1”];
cell.PutValue(“Sales”);
cell = cells[“A2”];
cell.PutValue(“Golf”);
cell = cells[“A3”];
cell.PutValue(“Golf”);
cell = cells[“A4”];
cell.PutValue(“Tennis”);
cell = cells[“A5”];
cell.PutValue(“Polo”);
cell = cells[“A6”];
cell.PutValue(“Golf”);
cell = cells[“A7”];
cell.PutValue(“Tennis”);
cell = cells[“A8”];
cell.PutValue(“Golf”);
cell = cells[“A9”];
cell.PutValue(“Polo”);
cell = cells[“A10”];
cell.PutValue(“Polo”);
cell = cells[“B2”];
cell.PutValue(“Qtr3”);
cell = cells[“B3”];
cell.PutValue(“Qtr4”);
cell = cells[“B4”];
cell.PutValue(“Qtr3”);
cell = cells[“B5”];
cell.PutValue(“Qtr3”);
cell = cells[“B6”];
cell.PutValue(“Qtr3”);
cell = cells[“B7”];
cell.PutValue(“Qtr4”);
cell = cells[“B8”];
cell.PutValue(“Qtr3”);
cell = cells[“B9”];
cell.PutValue(“Qtr4”);
cell = cells[“B10”];
cell.PutValue(“Qtr3”);
cell = cells[“C2”];
cell.PutValue(1500);
cell = cells[“C3”];
cell.PutValue(2000);
cell = cells[“C4”];
cell.PutValue(600);
cell = cells[“C5”];
cell.PutValue(1500);
cell = cells[“C6”];
cell.PutValue(4070);
cell = cells[“C7”];
cell.PutValue(5000);
cell = cells[“C8”];
cell.PutValue(6430);
cell = cells[“C9”];
cell.PutValue(12999);
cell = cells[“C10”];
cell.PutValue(8050);
sheet = workbook.Worksheets.Add(“pivot”);
var pivotTables = sheet.PivotTables;
//Adding a PivotTable to the worksheet
int index = pivotTables.Add("=Sheet1!A1:C10", “A1”, “PivotTable2”);
//Accessing the instance of the newly added PivotTable
var pivotTable = pivotTables[index];
//Showing grand totals for rows & columns
pivotTable.RowGrand = true;
pivotTable.ColumnGrand = true;
int rowFieldIndex = pivotTable.AddFieldToArea(PivotFieldType.Row, 0);
int colFieldIndex = pivotTable.AddFieldToArea(PivotFieldType.Column, 1);
PivotField cpf = pivotTable.ColumnFields[colFieldIndex];
int dataFieldIndex = pivotTable.AddFieldToArea(PivotFieldType.Data, 2);
PivotField pf = pivotTable.DataFields[dataFieldIndex];
pf.Function = ConsolidationFunction.Count;
PivotField rpf = pivotTable.RowFields[rowFieldIndex];
//Enable AutoSort
rpf.IsAutoSort = true;
//Sort Decending
rpf.IsAscendSort = false;
//Sort by Grand Total (Sum of Sales)
rpf.AutoSortField = 0;
pivotTable.IsAutoFormat = true;
workbook.Save(“D:/temp/output.xlsx”, SaveFormat.Xlsx);
In case you face any difficulty, please provide us the sample spreadsheet after processing the smart markers and refreshing the PivotTable with new data. We will further look into the matter to provide the exact code snippet according to PivotFields in the spreadsheet.