Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
将基础字段添加到数据(值)区域是塑造数据透视表如何聚合源数据的第一步。Aspose.Cells 公开了 PivotTable.AddFieldToArea(PivotFieldType, string),这是一个接受常量 PivotFieldType.Data 和源列名称的重载方法。字段添加到数据区域后,API 通过 PivotTable.DataFields 集合按字段被添加的顺序公开该字段。默认情况下,数值类型的源列使用 ConsolidationFunction.Sum 进行汇总,而非数值类型的列默认为 Count。
using System;
using Aspose.Cells;
using Aspose.Cells.Pivot;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Data";
string[] headers = { "Category", "Item", "Year", "Amount" };
for (int j = 0; j < headers.Length; j++)
{
worksheet.Cells[0, j].PutValue(headers[j]);
}
object[,] data = {
{ "Fruit", "Apple", 2020, 100 },
{ "Fruit", "Apple", 2021, 150 },
{ "Fruit", "Banana", 2020, 80 },
{ "Fruit", "Banana", 2021, 90 },
{ "Vegetable", "Carrot", 2020, 50 },
{ "Vegetable", "Carrot", 2021, 60 },
{ "Vegetable", "Daikon", 2020, 40 },
{ "Vegetable", "Daikon", 2021, 45 }
};
for (int i = 0; i < data.GetLength(0); i++)
{
for (int j = 0; j < data.GetLength(1); j++)
{
worksheet.Cells[i + 1, j].PutValue(data[i, j]);
}
}
int pivotIndex = worksheet.PivotTables.Add("A1:D9", "F3", "PivotTable1");
PivotTable pivotTable = worksheet.PivotTables[pivotIndex];
pivotTable.AddFieldToArea(PivotFieldType.Row, "Category");
pivotTable.AddFieldToArea(PivotFieldType.Row, "Item");
pivotTable.AddFieldToArea(PivotFieldType.Column, "Year");
pivotTable.AddFieldToArea(PivotFieldType.Data, "Amount");
pivotTable.CalculateData();
workbook.Save("output_drag.xlsx");
字段驻留在数据区域后,Aspose.Cells 通过 PivotTable.DataFields 上的 PivotField 对象公开该字段。每个 PivotField 都有一个类型为 ConsolidationFunction 的可写 Function 属性,用于控制应用于该字段基础值的聚合方式。ConsolidationFunction 是一个枚举,其成员包括 Sum、Count、Average、Max、Min、Product、StdDev、StdDevp、Var 和 Varp —— 前六个涵盖了绝大多数实际用例,而后四个是用于方差分析的统计聚合函数。
Function 仅影响聚合方式,源列以及数据透视表的行/列结构均不会被修改。要为现有数据字段切换聚合方式,请设置 pivotTable.DataFields[i].Function = ConsolidationFunction.<X>;,然后调用 pivotTable.CalculateData() 以重新渲染数据透视表。
using System;
using Aspose.Cells;
using Aspose.Cells.Pivot;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Data";
string[] headers = { "Category", "Item", "Year", "Amount" };
for (int j = 0; j < headers.Length; j++)
{
worksheet.Cells[0, j].PutValue(headers[j]);
}
object[,] data = {
{ "Fruit", "Apple", 2020, 100 },
{ "Fruit", "Apple", 2021, 150 },
{ "Fruit", "Banana", 2020, 80 },
{ "Fruit", "Banana", 2021, 90 },
{ "Vegetable", "Carrot", 2020, 50 },
{ "Vegetable", "Carrot", 2021, 60 },
{ "Vegetable", "Daikon", 2020, 40 },
{ "Vegetable", "Daikon", 2021, 45 }
};
for (int i = 0; i < data.GetLength(0); i++)
{
for (int j = 0; j < data.GetLength(1); j++)
{
worksheet.Cells[i + 1, j].PutValue(data[i, j]);
}
}
int pivotIndex = worksheet.PivotTables.Add("A1:D9", "F3", "PivotTable1");
PivotTable pivotTable = worksheet.PivotTables[pivotIndex];
pivotTable.AddFieldToArea(PivotFieldType.Row, "Category");
pivotTable.AddFieldToArea(PivotFieldType.Row, "Item");
pivotTable.AddFieldToArea(PivotFieldType.Column, "Year");
pivotTable.AddFieldToArea(PivotFieldType.Data, "Amount");
pivotTable.AddFieldToArea(PivotFieldType.Data, "Amount");
PivotField countField = pivotTable.DataFields[1];
countField.Function = ConsolidationFunction.Count;
pivotTable.CalculateData();
workbook.Save("output_function.xlsx");
当数据透视表包含两个或更多数据字段时,Aspose.Cells 会公开一个额外的虚拟字段 PivotTable.ValuesField。该虚拟字段表示驻留在数据区域中所有数据字段的聚合。您可以将其作为基础数据透视字段拖到行或列区域,这对于并排展示多个度量值非常有用。
PivotTable.ValuesField 不起作用。
using System;
using Aspose.Cells;
using Aspose.Cells.Pivot;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Data";
string[] headers = { "Category", "Item", "Year", "Amount" };
for (int j = 0; j < headers.Length; j++)
{
worksheet.Cells[0, j].PutValue(headers[j]);
}
object[,] data = {
{ "Fruit", "Apple", 2020, 100 },
{ "Fruit", "Apple", 2021, 150 },
{ "Fruit", "Banana", 2020, 80 },
{ "Fruit", "Banana", 2021, 90 },
{ "Vegetable", "Carrot", 2020, 50 },
{ "Vegetable", "Carrot", 2021, 60 },
{ "Vegetable", "Daikon", 2020, 40 },
{ "Vegetable", "Daikon", 2021, 45 }
};
for (int i = 0; i < data.GetLength(0); i++)
{
for (int j = 0; j < data.GetLength(1); j++)
{
worksheet.Cells[i + 1, j].PutValue(data[i, j]);
}
}
int pivotIndex = worksheet.PivotTables.Add("A1:D9", "F3", "PivotTable1");
PivotTable pivotTable = worksheet.PivotTables[pivotIndex];
pivotTable.AddFieldToArea(PivotFieldType.Row, "Category");
pivotTable.AddFieldToArea(PivotFieldType.Row, "Item");
pivotTable.AddFieldToArea(PivotFieldType.Column, "Year");
pivotTable.AddFieldToArea(PivotFieldType.Data, "Amount");
pivotTable.AddFieldToArea(PivotFieldType.Data, "Amount");
pivotTable.DataFields[1].Function = ConsolidationFunction.Count;
pivotTable.AddFieldToArea(PivotFieldType.Column, pivotTable.ValuesField.Name);
pivotTable.CalculateData();
workbook.Save("output_plot.xlsx");
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.