【问题标题】:How can I order/sort a PivotTable based on the contents of a DataField PivotField?如何根据 DataField PivotField 的内容对 PivotTable 进行排序/排序?
【发布时间】:2017-03-01 17:10:36
【问题描述】:

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

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”列降序排序。

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

...但它什么也没做。

【问题讨论】:

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


    【解决方案1】:

    我不明白为什么它有效,但它确实有效:

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

    我最好的猜测是描述“伞”(它有各种“子值”)中,自动排序是对总购买量进行排序,而“2”参数表示降序。无论如何,尽管它不直观且似乎没有记录,但它确实可以按我的意愿工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-07
      • 2017-07-18
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多