合并函数

合并函数

Aspose.Cells 可用于将合并函数应用于数据透视表的数据字段(或值字段)。在 Microsoft Excel 中,您可以右键单击值字段,然后选择“值字段设置…”选项,然后选择选项卡“按以下方式汇总值”。从那里,您可以选择任何您喜欢的合并函数,如求和、计数、平均值、最大值、最小值、乘积、去重计数等。

Aspose.Cells提供ConsolidationFunction枚举以支持以下合并功能。

  • ConsolidationFunction.Average
  • ConsolidationFunction.Count
  • ConsolidationFunction.CountNums
  • ConsolidationFunction.DistinctCount
  • ConsolidationFunction.Max
  • ConsolidationFunction.Min
  • ConsolidationFunction.Product
  • ConsolidationFunction.StdDev
  • ConsolidationFunction.StdDevp
  • ConsolidationFunction.Sum
  • ConsolidationFunction.Var
  • ConsolidationFunction.Varp

应用合并功能到数据字段的数据透视表

以下代码将平均值整合函数应用于第一个数据字段(或数值字段),将不重复计数整合函数应用于第二个数据字段(或数值字段)。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create workbook from source excel file
Workbook workbook = new Workbook(dataDir + "Book.xlsx");
// Access the first worksheet of the workbook
Worksheet worksheet = workbook.Worksheets[0];
// Access the first pivot table of the worksheet
PivotTable pivotTable = worksheet.PivotTables[0];
// Apply Average consolidation function to first data field
pivotTable.DataFields[0].Function = ConsolidationFunction.Average;
// Apply DistinctCount consolidation function to second data field
pivotTable.DataFields[1].Function = ConsolidationFunction.DistinctCount;
// Calculate the data to make changes affect
pivotTable.CalculateData();
// Saving the Excel file
workbook.Save(dataDir + "output.xlsx");