gpt4 book ai didi

c# - 如何根据 DataField PivotField 的内容对 PivotTable 进行排序/排序?

转载 作者:行者123 更新时间:2023-12-04 20:11:40 26 4
gpt4 key购买 nike

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

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

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

最佳答案

我不明白为什么它会起作用,但这确实:

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

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

关于c# - 如何根据 DataField PivotField 的内容对 PivotTable 进行排序/排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com