Modify Page Field Layout in Pivot Table
Introduction
A pivot table in Microsoft Excel exposes a dedicated page field area that sits above the row/column/data body of the table. This area is rendered as a strip of dropdown filter controls (one per page field) and is what end-users click to slice the pivot by criteria such as year or region. Aspose.Cells for Go via C++ models this area through the PivotTable.PageFields collection and exposes three properties that control how the strip is visually laid out:
PivotTable.PageFieldOrder(anAspose.Cells.PrintOrderTypevalue) decides whether additional page fields are placed next to the existing ones or below them.PivotTable.PageFieldWrapCountsets how many page fields are placed per row or column before wrapping.PivotTable.PageFields.Move(currIndex, destIndex)reorders the page fields without changing the order mode. This article walks through three code examples that demonstrate each of these operations on a shared dataset, so that you can compare the resulting layouts side-by-side.
Source Data
| Fruit | Year | Region | Amount |
|---|---|---|---|
| Apple | 2022 | North | 150 |
| Apple | 2023 | North | 180 |
| Banana | 2022 | South | 120 |
| Banana | 2023 | South | 140 |
| Cherry | 2022 | East | 200 |
| Cherry | 2023 | East | 220 |
| Grape | 2022 | West | 90 |
| Grape | 2023 | West | 110 |
| All eight rows are populated in every code example, in identical order, so the source data never differs between scenarios — only the page-field layout properties do. |
Example 1: Over Then Down
In the first scenario we configure the two page fields (Year, Region) to appear side-by-side in a single row at the top of the pivot table. We assign Fruit to the row axis, place Year first and Region second on the page axis (the order of AddFieldToArea calls determines the starting index), add Amount (Sum) as the data field, and then set PageFieldOrder to PrintOrderType.OverThenDown with PageFieldWrapCount = 2. With OverThenDown and a wrap count of 2, the two page fields are laid out horizontally side-by-side in a single row at the top of the pivot table, so the strip occupies one row of width two.
Example 2: Down Then Over
In this example we place Fruit on the row axis, Year and Region on the page axis (with Year first), and Amount (Sum) as the data field — exactly as in Example 1. We then set PageFieldOrder to PrintOrderType.DownThenOver and PageFieldWrapCount to 2. With DownThenOver and a wrap count of 2, the two page fields are stacked vertically — Year on top, Region directly below — forming a single column at the top of the pivot table. The strip therefore occupies two rows of width one, in contrast to Example 1.
Example 3: Move a Page Field
In the third scenario we keep this dataset and field allocation, set a neutral layout (OverThenDown with wrap count 2), and then demonstrate the PageFields.Move operation. The Move(0, 1) call moves the page field at index 0 (Year) to position 1, and the page field that was at position 1 (Region) shifts to position 0. After this call, Region is the first page field and Year is the second. The wrap and order mode are unchanged, so the strip is still rendered horizontally side-by-side — only the order of the two dropdowns has been swapped.
Related Articles
- Add Page Field in Pivot Table — the parent page that introduces how page fields are added to a pivot table.
- Row and Column Fields in Pivot Table — covers allocating fields to the row and column axes, complementing the page-axis work shown here.
- Manage Value Fields in Pivot Table — describes how to configure the data (value) area, including the
Sumaggregation used in this article. - Refresh Pivot Table — explains
RefreshDataandCalculateData, which are required after reordering page fields. - Apply Style to Pivot Table — shows how to format the rendered pivot table after the page-field strip has been laid out.