如何根据数据字段的内容对数据透视表进行排序?

本文关键字:数据 透视 排序 何根 字段 | 更新日期: 2023-09-27 18:13:57

我有一个数据透视表页面(源数据),用于在另一个工作表上创建数据透视表。我这样做:

var pch = _xlBook.PivotCaches();
int pivotDataRowsUsed = _xlBook.Worksheets["PivotData"].UsedRange.Rows.Count;
int pivotDataColsUsed = _xlBook.Worksheets["PivotData"].UsedRange.Columns.Count;
string lastColWrittenAsAlpha = ReportRunnerConstsAndUtils.GetExcelColumnName(pivotDataColsUsed);
string endRange = string.Format("{0}{1}", lastColWrittenAsAlpha, pivotDataRowsUsed);
Range sourceData = _xlBook.Worksheets["PivotData"].Range[string.Format("A1:{0}", endRange)];
PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A6"], "PivotTable");
pvt.MergeLabels = true; // The only thing I noticed this doing was centering the heading labels
pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField;
var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
monthField.NumberFormat = "mmm yyyy";
monthField.DataRange.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
// from http://stackoverflow.com/questions/40031858/how-can-i-change-the-text-of-the-automatically-added-labels-on-these-excel-inter
pvt.CompactLayoutColumnHeader = "Months";
pvt.CompactLayoutRowHeader = "Description";
pvt.AddDataField(pvt.PivotFields("TotalQty"), "Total Packages", XlConsolidationFunction.xlSum).NumberFormat = "###,##0";
pvt.AddDataField(pvt.PivotFields("TotalSales"), "Total Purchases", XlConsolidationFunction.xlSum).NumberFormat = "$#,##0";
PivotField avg = pvt.CalculatedFields().Add("Average Price", "=TotalSales/TotalQty", true);
avg.Orientation = XlPivotFieldOrientation.xlDataField;
// TODO: This calculation needs to change (it needs to actually be made a calculation, rather than just the TotalSales val)
pvt.CalculatedFields()._Add("PercentOfTotal", "=TotalSales");
pvt.AddDataField(pvt.PivotFields("PercentOfTotal"), "% of Total", Type.Missing).NumberFormat = "###.##";
// suggestion from http://stackoverflow.com/questions/40003146/how-can-i-get-this-pivottable-to-display-the-values-i-want-it-to-in-the-locatio
pvt.DataPivotField.Orientation = XlPivotFieldOrientation.xlRowField;

源数据按描述的字母顺序"排序",但不是真正/严格的。但是,源数据透视的数据透视是按Description的字母顺序排列的。

是否有一种方法可以防止数据透视表在输入源数据时被排序?如果是这样,我想最简单的事情就是按我想要的方式对源数据进行排序,按"TotalSales"列降序排序就足够简单了。

所以我要么需要改变我的源数据-如果我可以告诉数据透视表保留源数据的顺序,而不是试图以任何方式对其进行排序-或者我需要一种方法来排序数据透视表中的数据透视字段("TotalSales")。

更新

尝试按总销售额排序,我试了这样做:

pvt.AddDataField(pvt.PivotFields("TotalSales"), "Total Purchases", XlConsolidationFunction.xlSum).NumberFormat = "$#,##0";
pvt.AddDataField(pvt.PivotFields("TotalSales").SortDescending());

第二行完全是猜测,但它编译了;不过,这并不奇怪,它会导致运行时错误。

更新2

我也试过了:

pvt.PivotFields("TotalSales").AutoSort(2, "Total Purchases");

…但是它什么也没做

如何根据数据字段的内容对数据透视表进行排序?

我不明白为什么可以工作,但是这个可以:

var fld = ((PivotField)pvt.PivotFields("Description"));
fld.AutoSort(2, "Total Purchases");

我最好的猜测是中的描述"伞"(它有各种"子值"),AutoSort是对总购买进行排序,"2"参数表示降序。无论如何,尽管它不直观,而且看起来没有文档,但它确实按照我的要求工作。