简体   繁体   English

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

[英]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”)。

UPDATE UPDATE

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. 不出所料,它会导致运行时错误。

UPDATE 2 更新2

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.

相关问题 如何在 C# 中以特定的自定义顺序列出具有方向 xlColumnField 的数据透视字段 - How can I list a pivotfield with orientation xlColumnField in a specific custom order in C# 如何通过 DataField 在 GridView 中找到 BoundField? - How can I find a BoundField inside a GridView by DataField? 如何从C#(Microsoft.Office.Interop.Excel)中的数据透视表对象获取可见的PivotField对象 - How to get Visible PivotField Object from a PivotTable object in C#(Microsoft.Office.Interop.Excel) 如何按长度降序对字符串列表的内容进行排序? - How can I sort the contents of a string list by length descending? 如何预选要包含在数据透视表中的字段? - How can I pre-select the fields to be included in a PivotTable? VSTO Excel 2007数据透视表,在一个以上的列中有一个PivotField - VSTO Excel 2007 PivotTable, having a PivotField in more than one column C#:如何根据一列中的最大值按升序对二维数组中的行进行排序? - C#: How can I sort rows in 2D array in ascending order based on highest value in one column? 我如何对数组中目录的顺序进行排序? - How can i sort the order of the directories inside the array? 如何使用.NET中的IComparer更改排序顺序 - How can I change the sort order using an IComparer in .NET 按字母顺序对目录进行排序 - Sort Table of Contents in alphabetical order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM