我得到(免费)一个" Grand Total"我的数据透视表中的列:
For" TotalQty"和" TotalPrice"这很棒,但对于" AvgPrice"和" PrcntgOfTotal"没那么多。
如何让这些val显示第一种情况下的平均值而不是总和呢?
月份百分比是该月份总和的TotalPrice百分比(不是所有月份的总和);因此,GrandTotal.PrcntgOfTotal也不应该是月度值的平均值。
以下是我生成此数据透视表的代码:
private void PopulatePivotTableSheet()
{
string NORTHWEST_CORNER_OF_PIVOT_TABLE = "A6";
AddPrePivotTableDataToPivotTableSheet();
var dataRange = rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address];
dataRange.AutoFitColumns();
var pivotTable = pivotTableWorksheet.PivotTables.Add(
pivotTableWorksheet.Cells[NORTHWEST_CORNER_OF_PIVOT_TABLE],
dataRange,
"PivotTable");
pivotTable.MultipleFieldFilters = true;
//pivotTable.RowGrandTotals = true; <= default setting
//pivotTable.ColumGrandTotals = true; <= default setting
//pivotTable.RowGrandTotals = false; // this prevents the "Grand Total" column
//pivotTable.ColumGrandTotals = false; // this prevents the totals rows at the bottom
//pivotTable.Compact = true;
//pivotTable.CompactData = true;
pivotTable.GridDropZones = false;
pivotTable.Outline = false;
pivotTable.OutlineData = false;
pivotTable.ShowError = true;
pivotTable.ErrorCaption = "[error]";
pivotTable.ShowHeaders = true;
pivotTable.UseAutoFormatting = true;
pivotTable.ApplyWidthHeightFormats = true;
pivotTable.ShowDrill = true;
// Row field[s]
var descRowField = pivotTable.Fields["Description"];
pivotTable.RowFields.Add(descRowField);
// Column field[s]
var monthYrColField = pivotTable.Fields["MonthYr"];
pivotTable.ColumnFields.Add(monthYrColField);
// Data field[s]
var totQtyField = pivotTable.Fields["TotalQty"];
pivotTable.DataFields.Add(totQtyField);
var totPriceField = pivotTable.Fields["TotalPrice"];
pivotTable.DataFields.Add(totPriceField);
// Don't know how to calc these vals here, so had to put them on the data sheet
var avgPriceField = pivotTable.Fields["AvgPrice"];
pivotTable.DataFields.Add(avgPriceField);
var prcntgOfTotalField = pivotTable.Fields["PrcntgOfTotal"];
pivotTable.DataFields.Add(prcntgOfTotalField);
}
答案 0 :(得分:1)
pivotTable.DataFields[DATAFIELD NUM].Function =
OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Average
请注意, DATAFIELD NUM 是数据字段集合中的索引。定义于:
OfficeOpenXml.Table.PivotTable.ExcelPivotTableDataFieldCollection