Back to Devexpress

How to: Move a Pivot Table

wpf-115417-controls-and-libraries-spreadsheet-examples-pivot-tables-how-to-move-a-pivot-table.md

latest2.9 KB
Original Source

How to: Move a Pivot Table

  • Jul 25, 2021
  • 2 minutes to read

To move a PivotTable report, use the PivotTable.MoveTo method. You can place a pivot table in another location in the existing worksheet or move it to a new worksheet.

Note

If a cell range where you wish to place your report is a regular range containing data, it will be overwritten without warning. But if the destination cell range contains a pivot table or table, the System.InvalidOperationException will be thrown since the pivot table cannot overlap another table or a PivotTable report.

Move a Pivot Table to Another Location in the Existing Worksheet

View Example

vb
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
worksheet.Columns("A").WidthInCharacters = 40

' Change the pivot table location.
worksheet.PivotTables("PivotTable1").MoveTo(worksheet("A7"))
' Refresh the pivot table.
worksheet.PivotTables("PivotTable1").Cache.Refresh()
csharp
Worksheet worksheet = workbook.Worksheets["Report1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
worksheet.Columns["A"].WidthInCharacters = 40;

// Change the pivot table location.
worksheet.PivotTables["PivotTable1"].MoveTo(worksheet["A7"]);
// Refresh the pivot table.
worksheet.PivotTables["PivotTable1"].Cache.Refresh();

Move a Pivot Table to a New Worksheet

View Example

vb
Dim worksheet As Worksheet = workbook.Worksheets("Report1")

' Create a new worksheet.
Dim targetWorksheet As Worksheet = 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
csharp
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;