Back to Devexpress

PivotTableCollection.Add(PivotCache, CellRange) Method

officefileapi-devexpress-dot-spreadsheet-dot-pivottablecollection-dot-add-x28-devexpress-dot-spreadsheet-dot-pivotcache-devexpress-dot-spreadsheet-dot-cellrange-x29.md

latest7.7 KB
Original Source

PivotTableCollection.Add(PivotCache, CellRange) Method

Creates a new pivot table with the default name using the specified data cache and returns the newly created PivotTable object.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
PivotTable Add(
    PivotCache cache,
    CellRange location
)
vb
Function Add(
    cache As PivotCache,
    location As CellRange
) As PivotTable

Parameters

NameTypeDescription
cachePivotCache

A PivotCache object that specifies the pivot cache on which the pivot table is based.

| | location | CellRange |

A CellRange object that specifies a cell or cell range in the upper-left corner of the PivotTable report’s destination range.

|

Returns

TypeDescription
PivotTable

A PivotTable object that specifies the newly created pivot table.

|

Remarks

The following example demonstrates how to use the Add method to create a new pivot table based on the PivotCache of the existing PivotTable report.

View Example

csharp
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"]);

pivotTable.BeginUpdate();

// 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;

pivotTable.EndUpdate();
vb
Dim worksheet As Worksheet = workbook.Worksheets.Add()
workbook.Worksheets.ActiveWorksheet = worksheet

' Create a pivot table based on the specified PivotTable cache.
Dim cache As PivotCache = workbook.Worksheets("Report1").PivotTables("PivotTable1").Cache
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(cache, worksheet("B2"))

pivotTable.BeginUpdate()

' 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

pivotTable.BeginUpdate()

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(PivotCache, CellRange) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-spreadsheet-pivot-table-api-examples/CS/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotTableActions.cs#L39

csharp
PivotCache cache = workbook.Worksheets["Report1"].PivotTables["PivotTable1"].Cache;
PivotTable pivotTable = worksheet.PivotTables.Add(cache, worksheet["B2"]);

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotTableActions.cs#L35

csharp
PivotCache cache = workbook.Worksheets["Report1"].PivotTables["PivotTable1"].Cache;
PivotTable pivotTable = worksheet.PivotTables.Add(cache, worksheet["B2"]);

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableActions.cs#L45

csharp
PivotCache cache = workbook.Worksheets["Report1"].PivotTables["PivotTable1"].Cache;
PivotTable pivotTable = worksheet.PivotTables.Add(cache, worksheet["B2"]);

wpf-spreadsheet-pivot-table-api-examples/VB/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotTableActions.vb#L39

vb
Dim cache As PivotCache = workbook.Worksheets("Report1").PivotTables("PivotTable1").Cache
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(cache, worksheet("B2"))

winforms-spreadsheet-pivot-table-api/VB/SpreadsheetPivotTableExamples/CodeExamples/PivotTableActions.vb#L35

vb
Dim cache As PivotCache = workbook.Worksheets("Report1").PivotTables("PivotTable1").Cache
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(cache, worksheet("B2"))

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/PivotTableActions.vb#L41

vb
Dim cache As PivotCache = workbook.Worksheets("Report1").PivotTables("PivotTable1").Cache
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(cache, worksheet("B2"))

See Also

Pivot Table API

How to: Create a Pivot Table

PivotTableCollection Interface

PivotTableCollection Members

DevExpress.Spreadsheet Namespace