[英]How can I order/sort a PivotTable based on the contents of a DataField PivotField?
I have a page of PivotData (source data) that is used to create a PivotTable on another sheet. 我有一个PivotData页面(源数据),用于在另一个工作表上创建数据透视表。 I'm doing that like so:
我这样做是这样的:
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;
The source data is "sort of" in alphabetical order by description, but not really/strictly. 根据描述,源数据按字母顺序“排序”,但不是真正/严格。 The PivotTablization of the source data does alphabetize by Description, though.
源数据的PivotTablization 并通过描述按字母顺序排列,虽然。
Is there a way to prevent the PivotTable from being sorted at all when fed the source data? 在提供源数据时,有没有办法防止数据透视表被排序? If so, I imagine the easiest thing to do would be to sort the source data as I want it, which would be easy enough by sorting by the "TotalSales" column descending.
如果是这样,我想最容易做的就是按照我想要的方式对源数据进行排序,这可以通过“TotalSales”列降序排序来实现。
So I either need to change my source data - if I can tell the PivotTable to retain the order of the source data, and not try to sort it itself in any way - OR I need a way to order the data in the PivotTable by a specific DataField PivotField ("TotalSales"). 所以我要么改变我的源数据 - 如果我可以告诉数据透视表保留源数据的顺序,而不是尝试以任何方式对其自身进行排序 - 或者我需要一种方法来通过数据顺序对数据透视表中的数据进行排序特定的DataField PivotField(“TotalSales”)。
In an attempt to sort by TotalSales, I tried this: 在尝试按TotalSales排序时,我尝试了这个:
pvt.AddDataField(pvt.PivotFields("TotalSales"), "Total Purchases", XlConsolidationFunction.xlSum).NumberFormat = "$#,##0";
pvt.AddDataField(pvt.PivotFields("TotalSales").SortDescending());
The second line was a total guess, but it compiled; 第二行是一个总猜测,但它编译; not surprisingly, though, it results in a runtime error.
不出所料,它会导致运行时错误。
I tried this, too: 我也尝试了这个:
pvt.PivotFields("TotalSales").AutoSort(2, "Total Purchases");
...but it didn't do anything. ......但它没有做任何事情。
I don't understand why it works, but this does: 我不明白它为什么会起作用,但这样做:
var fld = ((PivotField)pvt.PivotFields("Description"));
fld.AutoSort(2, "Total Purchases");
My best guess is that within the Description "umbrella" (which has various "subvalues"), the AutoSort is sorting on Total Purchases, and the "2" arg means descending. 我最好的猜测是在描述“伞”(具有各种“子值”)中,AutoSort按总购买量排序,“2”arg表示降序。 At any rate, unintuitive and seemingly undocumented as it is, it does work as I want it to.
无论如何,不直观且看似没有文档,它确实按照我的意愿工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.