如何根据DataField PivotField的内容对数据透视表进行排序/排序?

时间:2016-10-19 21:37:39

标签: c# excel sorting pivot-table excel-interop

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

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;

源数据按描述按字母顺序“排序”,但不是真正/严格。但是,源数据的数据透视表实现按描述字母顺序排列。

在提供源数据时,有没有办法阻止数据透视表完全排序?如果是这样,我想最容易做的就是按照我想要的方式对源数据进行排序,这可以通过按“TotalSales”列降序排序来实现。

所以我需要更改我的源数据 - 如果我可以告诉数据透视表保留源数据的顺序,而不是尝试以任何方式对其自身进行排序 - 或者我需要一种方法来对数据进行排序由特定的DataField PivotField(“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");

......但它没有做任何事情。

1 个答案:

答案 0 :(得分:0)

我不明白为什么它有效,但这样做:

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

我最好的猜测是描述“伞”(有各种“子价值”)中,AutoSort按总购买量排序,“2”arg表示降序。无论如何,不​​直观且看似无证,它确实按照我的意愿工作。