合并函数
Contents
[
Hide
]
合并函数
Aspose.Cells 可用于将合并函数应用于数据透视表的数据字段(或值字段)。在 Microsoft Excel 中,您可以右键单击值字段,然后选择“值字段设置…”选项,然后选择选项卡“按以下方式汇总值”。从那里,您可以选择任何您喜欢的合并函数,如求和、计数、平均值、最大值、最小值、乘积、去重计数等。
Aspose.Cells提供ConsolidationFunction枚举以支持以下合并功能。
- ConsolidationFunction.SUM
- ConsolidationFunction.COUNT
- ConsolidationFunction.AVERAGE
- ConsolidationFunction.MAX
- ConsolidationFunction.MIN
- ConsolidationFunction.PRODUCT
- ConsolidationFunction.COUNT_NUMS
- ConsolidationFunction.STD_DEV
- ConsolidationFunction.STD_DEVP
- ConsolidationFunction.VAR
- ConsolidationFunction.VARP
- ConsolidationFunction.DISTINCT_COUNT
应用合并功能到数据字段的数据透视表
以下代码将AVERAGE合并函数应用于第一个数据字段(或值字段),并将STD_DEV合并函数应用于第二个数据字段(或值字段)。
可从此处下载示例源文件和输出文件以测试示例代码:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the resource directory | |
// Create workbook from source excel file | |
Workbook workbook = new Workbook("source.xlsx"); | |
// Access the second worksheet of the workbook | |
Worksheet worksheet = workbook.getWorksheets().get(1); | |
// Access the first pivot table of the worksheet | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Apply AVERAGE consolidation function to first data field | |
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.AVERAGE); | |
// Apply STD_DEV consolidation function to second data field | |
pivotTable.getDataFields().get(1).setFunction(ConsolidationFunction.STD_DEV); | |
// Calculate the data to make changes affect | |
pivotTable.calculateData(); | |
// Saving the Excel file | |
workbook.save("output.xlsx"); |
Microsoft Excel 2013仅支持去重计数合并功能。