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

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

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; 

源数据按描述按字母顺序“sorting”,但不是真的/严格的。 尽pipe如此,源数据的数据透视表按字母顺序排列。

有没有一种方法来防止数据透视表sorting源数据提供? 如果是这样,我想最简单的事情就是根据需要对源数据进行sorting,通过按“TotalSales”列降序进行sorting将很容易。

所以我要么改变我的源数据 – 如果我可以告诉数据透视表保留源数据的顺序,而不是尝试以任何方式自行sorting – 或者我需要一种方法来通过数据透视表sorting数据特定的DataField PivotField(“TotalSales”)。

UPDATE

在尝试按TotalSalessorting,我试过这个:

 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"); 

我最好的猜测是, “说明”(具有各种“子值”)内,自动sorting是对总采购进行sorting,而“2”参数意味着下降。 无论如何,它是不直观的,看起来没有logging的,它确实按照我的意图行事。