Example T501894
Visible to All Users

Spreadsheet Document API - Manage Pivot Tables in Code

This example demonstrates how to use the Spreadsheet Document API to create and modify pivot tables in code.

The application form contains the list of supported operations. A user can select an operation and click the Run button to view the resulting spreadsheet document in Microsoft Excel.

[!IMPORTANT]
The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. Please refer to the following page for pricing information: DevExpress Subscription

Files to Look At

C# Visual Basic
PivotCalculatedFieldActions.cs PivotCalculatedFieldActions.vb
PivotCalculatedItemActions.cs PivotCalculatedItemActions.vb
PivotFieldActions.cs PivotFieldActions.vb
PivotFieldGroupingActions.cs PivotFieldGroupingActions.vb
PivotTableActions.cs PivotTableActions.vb
PivotTableFilterActions.cs PivotTableFilterActions.vb
PivotTableFormattingActions.cs PivotTableFormattingActions.vb
PivotTableLayoutActions.cs PivotTableLayoutActions.vb
ValueFieldSettingsActions.cs ValueFieldSettingsActions.vb

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedFieldActions.cs(vb)
C#
using DevExpress.Charts.Model; using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { class PivotCalculatedFieldActions { public static Action<Workbook> AddCalculatedFieldAction = AddCalculatedField; public static Action<Workbook> RemoveCalculatedFieldAction = RemoveCalculatedField; public static Action<Workbook> ModifyCalculatedFieldAction = ModifyCalculatedField; static void AddCalculatedField(IWorkbook workbook) { #region #AddCalculatedField Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Create a calculated field based on data in the "Sales" field. PivotField field = pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax"); // Add the calculated field to the data area and specify the custom field name. PivotDataField dataField = pivotTable.DataFields.Add(field, "Total Tax"); // Specify the number format for the data field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; #endregion #AddCalculatedField } static void RemoveCalculatedField(IWorkbook workbook) { #region #RemoveCalculatedField Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Create a calculated field based on data in the "Sales" field. pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax"); // Access the calculated field by its name in the collection. PivotField field = pivotTable.CalculatedFields["Sales Tax"]; // Add the calculated field to the data area. PivotDataField dataField = pivotTable.DataFields.Add(field); //Remove the calculated field. pivotTable.CalculatedFields.RemoveAt(0); #endregion #RemoveCalculatedField } static void ModifyCalculatedField(IWorkbook workbook) { #region #ModifyCalculatedField Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Create a calculated field based on data in the "Sales" field. pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax Rate 10"); // Access the calculated field by its name in the collection. PivotField field = pivotTable.CalculatedFields["Sales Tax Rate 10"]; //Change the formula for the calculated field. field.Formula = "=Sales*15%"; //Change the calculated field name. field.Name = "Sales Tax Rate 15"; //Add the calculated field to the data area and specify the custom field name. PivotDataField dataField = pivotTable.DataFields.Add(field, "Total Tax"); // Specify the number format for the data field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; #endregion #ModifyCalculatedField } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedItemActions.cs(vb)
C#
using DevExpress.Charts.Model; using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { class PivotCalculatedItemActions { public static Action<Workbook> AddCalculatedItemAction = AddCalculatedItem; public static Action<Workbook> RemoveCalculatedItemAction = RemoveCalculatedItem; public static Action<Workbook> ModifyCalculatedItemAction = ModifyCalculatedItem; static void AddCalculatedItem(IWorkbook workbook) { #region #AddCalculatedItem Worksheet worksheet = workbook.Worksheets["Report10"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["State"]; // Add calculated items to the "State" field. field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total"); field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total"); #endregion #AddCalculatedItem } static void RemoveCalculatedItem(IWorkbook workbook) { #region #RemoveCalculatedItem Worksheet worksheet = workbook.Worksheets["Report7"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["Customer"]; // Add a calculated item to the "Customer" field. field.CalculatedItems.Add("='Big Foods'*110%", "Big Foods Sales Plan"); //Remove the calculated item by its index from the collection. field.CalculatedItems.RemoveAt(0); #endregion #RemoveCalculatedItem } static void ModifyCalculatedItem(IWorkbook workbook) { #region #ModifyCalculatedItem Worksheet worksheet = workbook.Worksheets["Report7"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["Customer"]; // Add a calculated item to the "Customer" field. PivotItem item = field.CalculatedItems.Add("='Big Foods'*110%", "Big Foods Sales Plan"); //Change the formula for the calculated item. item.Formula = "='Big Foods'*115%"; #endregion #ModifyCalculatedItem } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldActions.cs(vb)
C#
using DevExpress.Charts.Model; using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class PivotFieldActions { public static Action<Workbook> AddFieldToAxisAction = AddFieldToAxis; public static Action<Workbook> InsertFieldToAxisAction = InsertFieldToAxis; public static Action<Workbook> MoveFieldToAxisAction = MoveFieldToAxis; public static Action<Workbook> MoveFieldUpAction = MoveFieldUp; public static Action<Workbook> MoveFieldDownAction = MoveFieldDown; public static Action<Workbook> RemoveFieldFromAxisAction = RemoveFieldFromAxis; public static Action<Workbook> SortFieldItemsAction = SortFieldItems; public static Action<Workbook> SortFieldItemsByDataFieldAction = SortFieldItemsByDataField; public static Action<Workbook> MultipleSubtotalsAction = MultipleSubtotals; static void AddFieldToAxis(IWorkbook workbook) { #region #AddToAxis Worksheet sourceWorksheet = workbook.Worksheets["Data1"]; Worksheet worksheet = workbook.Worksheets.Add(); workbook.Worksheets.ActiveWorksheet = worksheet; // Create a pivot table. PivotTable pivotTable = worksheet.PivotTables.Add(sourceWorksheet["A1:D41"], worksheet["B2"]); // Add the "Product" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Product"]); // Add the "Category" field to the column axis area. pivotTable.ColumnFields.Add(pivotTable.Fields["Category"]); // Add the "Sales" field to the data area and specify the custom field name. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Sales"], "Sales(Sum)"); // Specify the number format for the "Sales" field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; // Add the "Region" field to the filter area. pivotTable.PageFields.Add(pivotTable.Fields["Region"]); #endregion #AddToAxis } static void InsertFieldToAxis(IWorkbook workbook) { #region #InsertAtTop Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Insert the "Region" field at the top of the row axis area. pivotTable.RowFields.Insert(0, pivotTable.Fields["Region"]); #endregion #InsertAtTop } static void MoveFieldToAxis(IWorkbook workbook) { #region #MoveToAxis Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Move the "Region" field to the column axis area. pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]); #endregion #MoveToAxis } static void MoveFieldUp(IWorkbook workbook) { #region #MoveUp Worksheet worksheet = workbook.Worksheets["Report3"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Move the "Category" field one position up in the row area. pivotTable.RowFields["Category"].MoveUp(); #endregion #MoveUp } static void MoveFieldDown(IWorkbook workbook) { #region #MoveDown Worksheet worksheet = workbook.Worksheets["Report3"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Move the "Region" field one position down in the row area. pivotTable.RowFields["Region"].MoveDown(); #endregion #MoveDown } static void RemoveFieldFromAxis(IWorkbook workbook) { #region #RemoveFromAxis Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Remove the "Product" field from the row axis area. pivotTable.RowFields.Remove(pivotTable.RowFields["Product"]); #endregion #RemoveFromAxis } static void SortFieldItems(IWorkbook workbook) { #region #SortFieldItems Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["Product"]; // Sort items in the "Product" field. field.SortType = PivotFieldSortType.Ascending; #endregion #SortFieldItems } static void SortFieldItemsByDataField(IWorkbook workbook) { #region #SortFieldItemsByDataField Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["Product"]; // Sort items in the "Product" field by values of the "Sum of Sales" data field. field.SortItems(PivotFieldSortType.Ascending, 0); #endregion #SortFieldItemsByDataField } static void MultipleSubtotals(IWorkbook workbook) { #region #MultipleSubtotals Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the pivot field by its name in the collection. PivotField field = pivotTable.Fields["Category"]; // Display multiple subtotals for the field. field.SetSubtotal(PivotSubtotalFunctions.Sum | PivotSubtotalFunctions.Average); #endregion #MultipleSubtotals } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldGroupingActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; using System.Collections.Generic; namespace SpreadsheetDocServerPivotAPI { class PivotFieldGroupingActions { public static Action<Workbook> GroupFieldItemsAction = GroupFieldItems; public static Action<Workbook> GroupFieldByDatesAction = GroupFieldByDates; public static Action<Workbook> UngroupSpecificItemAction = UngroupSpecificItem; public static Action<Workbook> UngroupFieldItemsAction = UngroupFieldItems; static void GroupFieldItems(IWorkbook workbook) { #region #GroupFieldItems Worksheet worksheet = workbook.Worksheets["Report11"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "State" field by its name in the collection. PivotField field = pivotTable.Fields["State"]; // Add the "State" field to the column axis area. pivotTable.ColumnFields.Add(field); // Group the first three items in the field. IEnumerable<int> items = new List<int>() { 0, 1, 2 }; field.GroupItems(items); // Access the created grouped field by its index in the field collection. int groupedFieldIndex = pivotTable.Fields.Count - 1; PivotField groupedField = pivotTable.Fields[groupedFieldIndex]; // Set the grouped item caption to "West". groupedField.Items[0].Caption = "West"; #endregion #GroupFieldItems } static void GroupFieldByDates(IWorkbook workbook) { #region #GroupFieldByDates Worksheet worksheet = workbook.Worksheets["Report8"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "DATE" field by its name in the collection. PivotField field = pivotTable.Fields["DATE"]; // Group field items by quarters and months. field.GroupItems(PivotFieldGroupByType.Quarters | PivotFieldGroupByType.Months); #endregion #GroupFieldByDates } static void GroupFieldByNumericRanges(IWorkbook workbook) { #region #GroupFieldByNumericRanges Worksheet worksheet = workbook.Worksheets["Report12"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "Usual Hours Worked" field by its name in the collection. PivotField field = pivotTable.Fields["Sales"]; // Group field items from 1000 to 4000 by 1000. field.GroupItems(1000, 4000, 1000, PivotFieldGroupByType.NumericRanges); #endregion #GroupFieldByNumericRanges } static void UngroupSpecificItem(IWorkbook workbook) { #region #UngroupSpecificItem Worksheet worksheet = workbook.Worksheets["Report11"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "State" field by its name in the collection. PivotField field = pivotTable.Fields["State"]; // Add the "State" field to the column axis area. pivotTable.ColumnFields.Add(field); // Group the first three items in the field. IEnumerable<int> items = new List<int>() { 0, 1, 2 }; field.GroupItems(items); // Access the created grouped field by its index in the field collection. int groupedFieldIndex = pivotTable.Fields.Count - 1; PivotField groupedField = pivotTable.Fields[groupedFieldIndex]; // Set the grouped item caption to "West". groupedField.Items[0].Caption = "West"; // Group the remaining field items. items = new List<int>() { 3, 4, 5 }; field.GroupItems(items); // Set the grouped item caption to "Midwest" groupedField.Items[1].Caption = "Midwest"; // Ungroup the "West" item. items = new List<int> { 0 }; groupedField.UngroupItems(items); #endregion #UngroupSpecificItem } static void UngroupFieldItems(IWorkbook workbook) { #region #UngroupFieldItems Worksheet worksheet = workbook.Worksheets["Report8"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "DATE" field by its name in the collection. PivotField field = pivotTable.Fields["DATE"]; // Group field items by days. field.GroupItems(field.GroupingInfo.DefaultStartValue, field.GroupingInfo.DefaultEndValue, 50, PivotFieldGroupByType.Days); // Ungroup field items. field.UngroupItems(); #endregion #UngroupFieldItems } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class PivotTableActions { public static Action<Workbook> CreatePivotTableFromRangeAction = CreatePivotTableFromRange; public static Action<Workbook> CreatePivotTableFromCacheAction = CreatePivotTableFromCache; public static Action<Workbook> RemovePivotTableAction = RemovePivotTable; public static Action<Workbook> ChangePivotTableLocationAction = ChangePivotTableLocation; public static Action<Workbook> MovePivotTableToWorksheetAction = MovePivotTableToWorksheet; public static Action<Workbook> ChangePivotTableDataSourceAction = ChangePivotTableDataSource; public static Action<Workbook> ClearPivotTableAction = ClearPivotTable; public static Action<Workbook> ChangeBehaviorOptionsAction = ChangeBehaviorOptions; static void CreatePivotTableFromRange(IWorkbook workbook) { #region #CreateFromRange Worksheet sourceWorksheet = workbook.Worksheets["Data1"]; Worksheet worksheet = workbook.Worksheets.Add(); workbook.Worksheets.ActiveWorksheet = worksheet; // Create a pivot table using the cell range "A1:D41" as the data source. PivotTable pivotTable = worksheet.PivotTables.Add(sourceWorksheet["A1:D41"], worksheet["B2"]); // Add the "Category" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Category"]); // Add the "Product" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Product"]); // Add the "Sales" field to the data area. pivotTable.DataFields.Add(pivotTable.Fields["Sales"]); #endregion #CreateFromRange } static void CreatePivotTableFromCache(IWorkbook workbook) { #region #CreateFromPivotCache Worksheet worksheet = workbook.Worksheets.Add(); workbook.Worksheets.ActiveWorksheet = worksheet; // Create a pivot table based on the specified PivotTable cache. PivotCache cache = workbook.Worksheets["Report1"].PivotTables["PivotTable1"].Cache; PivotTable pivotTable = worksheet.PivotTables.Add(cache, worksheet["B2"]); // Add the "Category" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Category"]); // Add the "Product" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Product"]); // Add the "Sales" field to the data area. pivotTable.DataFields.Add(pivotTable.Fields["Sales"]); // Set the default style for the pivot table. pivotTable.Style = workbook.TableStyles.DefaultPivotStyle; #endregion #CreateFromPivotCache } static void RemovePivotTable(IWorkbook workbook) { #region #RemoveTable Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Remove the pivot table from the collection. worksheet.PivotTables.Remove(pivotTable); #endregion #RemoveTable } static void ChangePivotTableLocation(IWorkbook workbook) { #region #ChangeLocation Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Change the pivot table location. worksheet.PivotTables["PivotTable1"].MoveTo(worksheet["A7"]); // Refresh the pivot table. worksheet.PivotTables["PivotTable1"].Cache.Refresh(); #endregion #ChangeLocation } static void MovePivotTableToWorksheet(IWorkbook workbook) { #region #MoveToWorksheet Worksheet worksheet = workbook.Worksheets["Report1"]; // Create a new worksheet. Worksheet targetWorksheet = workbook.Worksheets.Add(); // Access the pivot table by its name in the collection // and move it to the new worksheet. worksheet.PivotTables["PivotTable1"].MoveTo(targetWorksheet["B2"]); // Refresh the pivot table. targetWorksheet.PivotTables["PivotTable1"].Cache.Refresh(); workbook.Worksheets.ActiveWorksheet = targetWorksheet; #endregion #MoveToWorksheet } static void ChangePivotTableDataSource(IWorkbook workbook) { #region #ChangeDataSource Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; Worksheet sourceWorksheet = workbook.Worksheets["Data2"]; // Change the data source of the pivot table. pivotTable.ChangeDataSource(sourceWorksheet["A1:H6367"]); // Add the "State" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["State"]); // Add the "Yearly Earnings" field to the data area. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Yearly Earnings"]); // Calculate the average of the "Yearly Earnings" values for each state. dataField.SummarizeValuesBy = PivotDataConsolidationFunction.Average; #endregion #ChangeDataSource } static void ClearPivotTable(IWorkbook workbook) { #region #ClearTable Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Clear the pivot table. worksheet.PivotTables["PivotTable1"].Clear(); #endregion #ClearTable } static void ChangeBehaviorOptions(IWorkbook workbook) { #region #ChangeBehaviorOptions Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; worksheet.Columns["B"].WidthInCharacters = 40; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Restrict specific operations for the pivot table. PivotBehaviorOptions behaviorOptions = pivotTable.Behavior; behaviorOptions.AutoFitColumns = false; behaviorOptions.EnableFieldList = false; // Refresh the pivot table. pivotTable.Cache.Refresh(); #endregion #ChangeBehaviorOptions } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableFilterActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class PivotTableFilterActions { public static Action<Workbook> SetItemFilterAction = SetItemFilter; public static Action<Workbook> SetItemVisibilityFilterAction = SetItemVisibilityFilter; public static Action<Workbook> SetLabelFilterAction = SetLabelFilter; public static Action<Workbook> SetValueFilterAction = SetValueFilter; public static Action<Workbook> SetTop10FilterAction = SetTop10Filter; public static Action<Workbook> SetDateFilterAction = SetDateFilter; public static Action<Workbook> SetMultipleFilterAction = SetMultipleFilter; static void SetItemFilter(IWorkbook workbook) { #region #ItemFilter Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Show the first item in the "Product" field. pivotTable.Fields[1].ShowSingleItem(0); //Show all items in the "Product" field (the default option). //pivotTable.Fields[1].ShowAllItems(); #endregion #ItemFilter } static void SetItemVisibilityFilter(IWorkbook workbook) { #region #ItemVisibility Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access items of the "Product" field. PivotItemCollection pivotFieldItems = pivotTable.Fields[1].Items; // Hide the first item in the "Product" field. pivotFieldItems[0].Visible = false; #endregion #ItemVisibility } static void SetLabelFilter(IWorkbook workbook) { #region #LabelFilter Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "Region" field. PivotField field = pivotTable.Fields[0]; // Filter the "Region" field by text to display sales data for the "South" region. pivotTable.Filters.Add(field, PivotFilterType.CaptionEqual, "South"); #endregion #LabelFilter } static void SetValueFilter(IWorkbook workbook) { #region #ValueFilter Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "Product" field. PivotField field = pivotTable.Fields[1]; // Filter the "Product" field to display products with grand total sales between $6000 and $13000. pivotTable.Filters.Add(field, pivotTable.DataFields[0], PivotFilterType.ValueBetween, 6000, 13000); #endregion #ValueFilter } static void SetTop10Filter(IWorkbook workbook) { #region #Top10Filter Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "Product" field. PivotField field = pivotTable.Fields[1]; // Filter the "Product" field to display two products with the lowest sales. PivotFilter filter = pivotTable.Filters.Add(field, pivotTable.DataFields[0], PivotFilterType.Count, 2); filter.Top10Type = PivotFilterTop10Type.Bottom; #endregion #Top10Filter } static void SetDateFilter(IWorkbook workbook) { #region #DateFilter Worksheet worksheet = workbook.Worksheets["Report6"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the "Date" field. PivotField field = pivotTable.Fields[0]; // Filter the "Date" field to display sales for the second quarter. pivotTable.Filters.Add(field, PivotFilterType.SecondQuarter); #endregion #DateFilter } static void SetMultipleFilter(IWorkbook workbook) { #region #MultipleFilters Worksheet worksheet = workbook.Worksheets["Report6"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Allow multiple filters for a field. pivotTable.Behavior.AllowMultipleFieldFilters = true; // Filter the "Date" field to display sales for the second quarter. PivotField field1 = pivotTable.Fields[0]; pivotTable.Filters.Add(field1, PivotFilterType.SecondQuarter); // Add the second filter to the "Date" field to display two days with the lowest sales. PivotFilter filter = pivotTable.Filters.Add(field1, pivotTable.DataFields[0], PivotFilterType.Count, 2); filter.Top10Type = PivotFilterTop10Type.Bottom; #endregion #MultipleFilters } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableFormattingActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class PivotTableFormattingActions { public static Action<Workbook> ChangeStylePivotTableAction = ChangeStylePivotTable; public static Action<Workbook> BandedColumnsAction = BandedColumns; public static Action<Workbook> BandedRowsAction = BandedRows; public static Action<Workbook> ShowColumnHeadersAction = ShowColumnHeaders; public static Action<Workbook> ShowRowHeadersAction = ShowRowHeaders; static void ChangeStylePivotTable(IWorkbook workbook) { #region #SetStyle Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Set the pivot table style. pivotTable.Style = workbook.TableStyles[BuiltInPivotStyleId.PivotStyleDark7]; #endregion #SetStyle } static void BandedColumns(IWorkbook workbook) { #region #BandedColumns Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Apply the banded column formatting to the pivot table. pivotTable.BandedColumns = true; #endregion #BandedColumns } static void BandedRows(IWorkbook workbook) { #region #BandedRows Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Apply the banded row formatting to the pivot table. pivotTable.BandedRows = true; #endregion #BandedRows } static void ShowColumnHeaders(IWorkbook workbook) { #region #ColumnHeaders Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Remove formatting from column headers. pivotTable.ShowColumnHeaders = false; #endregion #ColumnHeaders } static void ShowRowHeaders(IWorkbook workbook) { #region #RowHeaders Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Region" field to the column axis area. pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]); // Remove formatting from row headers. pivotTable.ShowRowHeaders = false; #endregion #RowHeaders } } }
SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableLayoutActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class PivotTableLayoutActions { public static Action<Workbook> ColumnGrandTotalsAction = ColumnGrandTotals; public static Action<Workbook> RowGrandTotalsAction = RowGrandTotals; public static Action<Workbook> DataOnRowsAction = DataOnRows; public static Action<Workbook> MergeTitlesAction = MergeTitles; public static Action<Workbook> ShowAllSubtotalsAction = ShowAllSubtotals; public static Action<Workbook> HideAllSubtotalsAction = HideAllSubtotals; public static Action<Workbook> SetCompactReportLayoutAction = SetCompactReportLayout; public static Action<Workbook> SetOutlineReportLayoutAction = SetOutlineReportLayout; public static Action<Workbook> SetTabularReportLayoutAction = SetTabularReportLayout; public static Action<Workbook> RepeatAllItemLabelsAction = RepeatAllItemLabels; public static Action<Workbook> InsertBlankRowsAction = InsertBlankRows; public static Action<Workbook> RemoveBlankRowsAction = RemoveBlankRows; static void ColumnGrandTotals(IWorkbook workbook) { #region #ColumnGrandTotals Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Region" field to the column axis area. pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]); // Hide grand totals for columns. pivotTable.Layout.ShowColumnGrandTotals = false; #endregion #ColumnGrandTotals } static void RowGrandTotals(IWorkbook workbook) { #region #RowGrandTotals Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Region" field to the column axis area. pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]); // Hide grand totals for rows. pivotTable.Layout.ShowRowGrandTotals = false; #endregion #RowGrandTotals } static void DataOnRows(IWorkbook workbook) { #region #MultipleDataFields Worksheet worksheet = workbook.Worksheets["Report2"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display value fields in separate columns. pivotTable.Layout.DataOnRows = false; #endregion #MultipleDataFields } static void MergeTitles(IWorkbook workbook) { #region #MergeTitles Worksheet worksheet = workbook.Worksheets["Report4"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display the pivot table in the tabular form. pivotTable.Layout.SetReportLayout(PivotReportLayout.Tabular); // Merge and center cells with labels. pivotTable.Layout.MergeTitles = true; #endregion #MergeTitles } static void ShowAllSubtotals(IWorkbook workbook) { #region #ShowAllSubtotals Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Show all subtotals at the top of each group. pivotTable.Layout.ShowAllSubtotals(true); #endregion #ShowAllSubtotals } static void HideAllSubtotals(IWorkbook workbook) { #region #HideAllSubtotals Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Hide subtotals at the top of each group. pivotTable.Layout.HideAllSubtotals(); #endregion #HideAllSubtotals } static void SetCompactReportLayout(IWorkbook workbook) { #region #CompactReportLayout Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display the pivot table in the compact form. pivotTable.Layout.SetReportLayout(PivotReportLayout.Compact); #endregion #CompactReportLayout } static void SetOutlineReportLayout(IWorkbook workbook) { #region #OutlineReportLayout Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display the pivot table in the outline form. pivotTable.Layout.SetReportLayout(PivotReportLayout.Outline); #endregion #OutlineReportLayout } static void SetTabularReportLayout(IWorkbook workbook) { #region #TabularReportLayout Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display the pivot table in the tabular form. pivotTable.Layout.SetReportLayout(PivotReportLayout.Tabular); #endregion #TabularReportLayout } static void RepeatAllItemLabels(IWorkbook workbook) { #region #RepeatAllItemLabels Worksheet worksheet = workbook.Worksheets["Report5"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Display repeated column labels. pivotTable.Layout.RepeatAllItemLabels(true); #endregion #RepeatAllItemLabels } static void InsertBlankRows(IWorkbook workbook) { #region #InsertBlankRows Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Insert a blank row after each group of items. pivotTable.Layout.InsertBlankRows(); #endregion #InsertBlankRows } static void RemoveBlankRows(IWorkbook workbook) { #region #RemoveBlankRows Worksheet worksheet = workbook.Worksheets["Report1"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection. PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Insert a blank row after each group of items. pivotTable.Layout.InsertBlankRows(); // Remove a blank row after each group of items. pivotTable.Layout.RemoveBlankRows(); #endregion #RemoveBlankRows } } }
SpreadsheetDocServerPivotAPI/CodeExamples/ValueFieldSettingsActions.cs(vb)
C#
using DevExpress.Spreadsheet; using System; namespace SpreadsheetDocServerPivotAPI { public static class ValueFieldSettingsActions { public static Action<Workbook> ChangeSummaryFunctionAction = ChangeSummaryFunction; public static Action<Workbook> DifferenceFromAction = DifferenceFrom; public static Action<Workbook> PercentOfAction = PercentOf; public static Action<Workbook> PercentOfParentRowTotalAction = PercentOfParentRowTotal; public static Action<Workbook> RankLargestToSmallestAction = RankLargestToSmallest; public static Action<Workbook> RunningTotalInAction = RunningTotalIn; public static Action<Workbook> NumberFormatAction = NumberFormat; static void ChangeSummaryFunction(IWorkbook workbook) { #region #ChangeSummaryFunction Worksheet sourceWorksheet = workbook.Worksheets["Data5"]; Worksheet worksheet = workbook.Worksheets.Add(); workbook.Worksheets.ActiveWorksheet = worksheet; // Create a pivot table using the cell range "A1:E65" as the data source. PivotTable pivotTable = worksheet.PivotTables.Add(sourceWorksheet["A1:E65"], worksheet["B2"]); // Add the "Category" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Category"]); // Add the "Product" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Product"]); // Add the "Amount" field to the data area. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"]); // Use the "Average" function to summarize values in the data field. dataField.SummarizeValuesBy = PivotDataConsolidationFunction.Average; // Specify the number format for the data field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; #endregion #ChangeSummaryFunction } static void DifferenceFrom(IWorkbook workbook) { #region #DifferenceFrom Worksheet worksheet = workbook.Worksheets["Report14"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the data field by its index in the collection. PivotDataField dataField = pivotTable.DataFields[0]; // Display the difference in product sales between the current quarter and the previous quarter. dataField.ShowValuesWithCalculation(PivotShowValuesAsType.Difference, pivotTable.Fields["Quarter"], PivotBaseItemType.Previous); #endregion #DifferenceFrom } static void PercentOf(IWorkbook workbook) { #region #PercentOf Worksheet worksheet = workbook.Worksheets["Report14"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Access the data field by its index in the collection. PivotDataField dataField = pivotTable.DataFields[0]; // Select the base field ("Quarter"). PivotField baseField = pivotTable.Fields["Quarter"]; // Select the base item ("Q1"). PivotItem baseItem = baseField.Items[0]; // Show values as the percentage of the value of the base item in the base field. dataField.ShowValuesWithCalculation(PivotShowValuesAsType.Percent, baseField, baseItem); #endregion #PercentOf } static void PercentOfParentRowTotal(IWorkbook workbook) { #region #PercentOfParentRowTotal Worksheet worksheet = workbook.Worksheets["Report16"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Amount" field to the data area for the second time and assign the custom name to the field. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"], "% of Parent Row Total"); // Show sales values for each product as the percentage of its category total. // Total values for each category are displayed as the percentage of the Grand Total value. dataField.ShowValuesWithCalculation(PivotShowValuesAsType.PercentOfParentRow); #endregion #PercentOfParentRowTotal } static void RankLargestToSmallest(IWorkbook workbook) { #region #RankLargestToSmallest Worksheet worksheet = workbook.Worksheets["Report13"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Amount" field to the data area for the second time and assign the custom name to the field. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"], "Rank"); // Display the rank of sales values for the "Customer" field, listing the largest item in the field as 1. dataField.ShowValuesWithCalculation(PivotShowValuesAsType.RankDescending, pivotTable.Fields["Customer"]); #endregion #RankLargestToSmallest } static void RunningTotalIn(IWorkbook workbook) { #region #RunningTotalIn Worksheet worksheet = workbook.Worksheets["Report15"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Access the pivot table by its name in the collection PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // Add the "Amount" field to the data area for the second time and assign the custom name to the field. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"], "Running Total"); // Display values for successive items in the "Quarter" field as a running total. dataField.ShowValuesWithCalculation(PivotShowValuesAsType.RunningTotal, pivotTable.Fields["Quarter"]); // Specify the number format for the data field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; #endregion #RunningTotalIn } static void NumberFormat(IWorkbook workbook) { #region #NumberFormat Worksheet sourceWorksheet = workbook.Worksheets["Data5"]; Worksheet worksheet = workbook.Worksheets.Add(); workbook.Worksheets.ActiveWorksheet = worksheet; // Create a pivot table using the cell range "A1:E65" as the data source. PivotTable pivotTable = worksheet.PivotTables.Add(sourceWorksheet["A1:E65"], worksheet["B2"]); // Add the "Category" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Category"]); // Add the "Product" field to the row axis area. pivotTable.RowFields.Add(pivotTable.Fields["Product"]); // Add the "Amount" field to the data area. PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"]); // Specify the number format for the data field. dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"; #endregion #NumberFormat } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.