[]
Gets the name of the data field used to sort this PivotTable field automatically.
If automatic sorting is configured by AutoSort(SortOrder?, string), this property returns the sort key field name that was specified for that rule.
string AutoSortField { get; }
ReadOnly Property AutoSortField As String
worksheet.Range["A1:C5"].Value = new object[,] {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Orange", "West", 80},
{"Apple", "West", 150},
{"Orange", "East", 90}
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField regionField = pivotTable.PivotFields["Region"];
regionField.Orientation = PivotFieldOrientation.RowField;
IPivotField amountField = pivotTable.PivotFields["Amount"];
pivotTable.AddDataField(amountField, "Sum of Amount", ConsolidationFunction.Sum);
regionField.AutoSort(SortOrder.Descending, amountField.SourceName);
string autoSortField = regionField.AutoSortField;