[]
Groups the PivotField by date or time units.
void Group(PivotFieldDateGroupOptions options)
Sub Group(options As PivotFieldDateGroupOptions)
| Type | Name | Description |
|---|---|---|
| PivotFieldDateGroupOptions | options | Date grouping settings. |
Workbook workbook = new Workbook();
workbook.Options.Data.AutomaticGroupDateTimeInPivotTable = false;
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
{ "Order Date", "Region", "Amount" },
{ new DateTime(2024, 1, 5), "East", 10 },
{ new DateTime(2024, 1, 18), "West", 20 },
{ new DateTime(2024, 2, 2), "East", 30 },
{ new DateTime(2024, 2, 25), "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField orderDateField = pivotTable.PivotFields["Order Date"];
orderDateField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldDateGroupOptions options = new PivotFieldDateGroupOptions
{
GroupBy = PivotFieldDateGroupBy.Months,
AutoStart = true,
AutoEnd = true
};
orderDateField.Group(options);
string groupedFieldName = pivotTable.RowFields[0].Name;
Groups the PivotField into numeric ranges.
void Group(PivotFieldNumberGroupOptions options)
Sub Group(options As PivotFieldNumberGroupOptions)
| Type | Name | Description |
|---|---|---|
| PivotFieldNumberGroupOptions | options | Numeric grouping settings. |
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
{ "Order ID", "Region", "Amount" },
{ 10348, "East", 10 },
{ 10362, "East", 20 },
{ 10375, "West", 30 },
{ 10391, "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField orderIdField = pivotTable.PivotFields["Order ID"];
orderIdField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldNumberGroupOptions options = new PivotFieldNumberGroupOptions
{
Start = 10348,
End = 10407,
AutoStart = false,
AutoEnd = false,
Interval = 20
};
orderIdField.Group(options);
Creates a custom group from PivotItems in the PivotField.
void Group(PivotFieldCustomGroupOptions options)
Sub Group(options As PivotFieldCustomGroupOptions)
| Type | Name | Description |
|---|---|---|
| PivotFieldCustomGroupOptions | options | The custom grouping settings. Must not be null. |
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
{ "Product", "Region", "Amount" },
{ "Apple", "East", 10 },
{ "Banana", "West", 20 },
{ "Carrot", "East", 30 },
{ "Date", "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField productField = pivotTable.PivotFields["Product"];
productField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldCustomGroupOptions options = new PivotFieldCustomGroupOptions
{
Name = "Fruit Group",
Items = new List<string> { "Apple", "Banana" }
};
productField.Group(options);
| Type | Condition |
|---|---|
| ArgumentNullException | If |
| NotSupportedException | If this field cannot be grouped. |