Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
.xls files) and modern named or custom pivot table styles (intended for .xlsx, .xlsm, and .xlsb files). The API you should call depends on the file format the workbook is saved to, not the format it was loaded from.
Aspose.Cells exposes two parallel style APIs for pivot tables. The decision between them is driven by the file format you save the workbook to, not by the format you read it from. A workbook loaded from an .xls file can be re-saved as .xlsx, and in that case the modern style API applies rather than the legacy one.
For legacy .xls output, use the PivotTable.AutoFormatType property together with the Aspose.Cells.Pivot.PivotTableAutoFormatType enumeration. This API corresponds to the autoformat picker that classic Excel offered for pivot tables.
For modern .xlsx, .xlsm, and .xlsb output, two flavors of style API are available:
PivotTable.PivotTableStyleType selects one of the built-in named styles (light and dark themes, including the styles added in Excel 2017). These presets are read-only.PivotTable.PivotTableStyleName selects a custom style you define yourself through Worksheets.TableStyles.AddPivotTableStyle(...). Custom styles are required whenever you want to modify colors, borders, or fonts beyond what the presets offer.In addition, PivotTable.FormatAll(Style) is a shortcut that applies a single Style object to every cell of the pivot, overriding whatever is set through either of the style-name APIs above. This is useful when a uniform appearance is required regardless of the underlying theme.
PivotTable.AutoFormatType accepts a value from the Aspose.Cells.Pivot.PivotTableAutoFormatType enumeration. The available values are Report1 through Report10, Classic, and Table1 through Table10.
The following example loads a fresh workbook, populates the Fruit/Year/Amount sample data, adds a pivot table, applies PivotTableAutoFormatType.Report5, and saves the result as .xls.
Report1 through Report10, Table1 through Table10) were designed in classic Excel for single-dimension pivot tables with row fields and values only — they have no built-in styling for column-field headers. If your pivot needs column fields, use the modern PivotTableStyleType presets from Scenario 2 instead, which are designed for the two-dimensional layout modern Excel uses.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet sheet = workbook.GetWorksheets().Get(0);
// Populate the source data with header row (Fruit, Year, Amount)
// and 9 data rows covering grape, blueberry, kiwi, cherry across 2020 and 2021
sheet.GetCells().Get(0, 0).PutValue(u"Fruit");
sheet.GetCells().Get(0, 1).PutValue(u"Year");
sheet.GetCells().Get(0, 2).PutValue(u"Amount");
sheet.GetCells().Get(1, 0).PutValue(u"grape");
sheet.GetCells().Get(1, 1).PutValue(2020);
sheet.GetCells().Get(1, 2).PutValue(50);
sheet.GetCells().Get(2, 0).PutValue(u"blueberry");
sheet.GetCells().Get(2, 1).PutValue(2020);
sheet.GetCells().Get(2, 2).PutValue(30);
sheet.GetCells().Get(3, 0).PutValue(u"kiwi");
sheet.GetCells().Get(3, 1).PutValue(2020);
sheet.GetCells().Get(3, 2).PutValue(25);
sheet.GetCells().Get(4, 0).PutValue(u"cherry");
sheet.GetCells().Get(4, 1).PutValue(2020);
sheet.GetCells().Get(4, 2).PutValue(40);
sheet.GetCells().Get(5, 0).PutValue(u"grape");
sheet.GetCells().Get(5, 1).PutValue(2021);
sheet.GetCells().Get(5, 2).PutValue(60);
sheet.GetCells().Get(6, 0).PutValue(u"blueberry");
sheet.GetCells().Get(6, 1).PutValue(2021);
sheet.GetCells().Get(6, 2).PutValue(35);
sheet.GetCells().Get(7, 0).PutValue(u"kiwi");
sheet.GetCells().Get(7, 1).PutValue(2021);
sheet.GetCells().Get(7, 2).PutValue(28);
sheet.GetCells().Get(8, 0).PutValue(u"cherry");
sheet.GetCells().Get(8, 1).PutValue(2021);
sheet.GetCells().Get(8, 2).PutValue(45);
sheet.GetCells().Get(9, 0).PutValue(u"grape");
sheet.GetCells().Get(9, 1).PutValue(2020);
sheet.GetCells().Get(9, 2).PutValue(45);
// Add a pivot table at destination cell E3, named "Pivot1", using source range A1:C10
int pivotIndex = sheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
PivotTable pivotTable = sheet.GetPivotTables().Get(pivotIndex);
// Assign fields: Fruit -> Rows, Amount -> Data
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
// Apply the legacy XLS preset autoformat "Report5"
pivotTable.SetAutoFormatType(PivotTableAutoFormatType::Report5);
// Save the workbook in legacy .xls format
workbook.Save(u"output.xls");
Aspose::Cells::Cleanup();
return 0;
}
This is the recommended API for any modern file format. Unlike the legacy autoformat, the style selected here is rendered faithfully by Excel and survives round-trips through other Office tooling.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
Cells cells = worksheet.GetCells();
cells.Get(u"A1").PutValue(u"Fruit");
cells.Get(u"B1").PutValue(u"Year");
cells.Get(u"C1").PutValue(u"Amount");
cells.Get(u"A2").PutValue(u"Grape");
cells.Get(u"B2").PutValue(2020);
cells.Get(u"C2").PutValue(100);
cells.Get(u"A3").PutValue(u"Blueberry");
cells.Get(u"B3").PutValue(2020);
cells.Get(u"C3").PutValue(150);
cells.Get(u"A4").PutValue(u"Kiwi");
cells.Get(u"B4").PutValue(2020);
cells.Get(u"C4").PutValue(200);
cells.Get(u"A5").PutValue(u"Cherry");
cells.Get(u"B5").PutValue(2020);
cells.Get(u"C5").PutValue(180);
cells.Get(u"A6").PutValue(u"Grape");
cells.Get(u"B6").PutValue(2021);
cells.Get(u"C6").PutValue(120);
cells.Get(u"A7").PutValue(u"Blueberry");
cells.Get(u"B7").PutValue(2021);
cells.Get(u"C7").PutValue(170);
cells.Get(u"A8").PutValue(u"Kiwi");
cells.Get(u"B8").PutValue(2021);
cells.Get(u"C8").PutValue(210);
cells.Get(u"A9").PutValue(u"Cherry");
cells.Get(u"B9").PutValue(2021);
cells.Get(u"C9").PutValue(190);
cells.Get(u"A10").PutValue(u"Grape");
cells.Get(u"B10").PutValue(2021);
cells.Get(u"C10").PutValue(130);
int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
pivotTable.SetPivotTableStyleType(PivotTableStyleType::PivotTableStyleDark1);
workbook.Save(u"output.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
The built-in presets cannot be modified. Whenever you need to override colors, borders, or fonts, you must define a custom pivot style. The workflow has three steps:
TableStyles collection via Worksheets.TableStyles.AddPivotTableStyle(string name). This returns the index of the newly created style.WholeTable or GrandTotalRow) through TableStyle.TableStyleElements.Add(TableStyleElementType), then assign a Style to each element via TableStyleElement.SetElementStyle(Style).PivotTable.PivotTableStyleName to the style’s name. Do not use PivotTableStyleType here, since that property selects built-in presets.PivotTableStyleName and PivotTableStyleType are not interchangeable. Use PivotTableStyleType for built-in presets, and PivotTableStyleName for custom styles you have defined through AddPivotTableStyle. Setting both is harmless, but only the one matching the intended source is rendered.
The available TableStyleElementType values include WholeTable, FirstRow, LastRow, FirstColumn, LastColumn, GrandTotalRow, GrandTotalColumn, PageFieldLabels, and PageFieldValues.
The following example defines a custom pivot style with a thin black border on WholeTable and a bold red font on GrandTotalRow, then applies it via PivotTableStyleName and saves as .xlsx.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
Cells cells = worksheet.GetCells();
// Populate source data: header row + 9 data rows (A1:C10)
cells.Get(u"A1").PutValue(u"Fruit");
cells.Get(u"B1").PutValue(u"Year");
cells.Get(u"C1").PutValue(u"Amount");
cells.Get(u"A2").PutValue(u"Grape");
cells.Get(u"B2").PutValue(2020);
cells.Get(u"C2").PutValue(100);
cells.Get(u"A3").PutValue(u"Blueberry");
cells.Get(u"B3").PutValue(2020);
cells.Get(u"C3").PutValue(200);
cells.Get(u"A4").PutValue(u"Kiwi");
cells.Get(u"B4").PutValue(2020);
cells.Get(u"C4").PutValue(300);
cells.Get(u"A5").PutValue(u"Cherry");
cells.Get(u"B5").PutValue(2020);
cells.Get(u"C5").PutValue(400);
cells.Get(u"A6").PutValue(u"Grape");
cells.Get(u"B6").PutValue(2021);
cells.Get(u"C6").PutValue(500);
cells.Get(u"A7").PutValue(u"Blueberry");
cells.Get(u"B7").PutValue(2021);
cells.Get(u"C7").PutValue(600);
cells.Get(u"A8").PutValue(u"Kiwi");
cells.Get(u"B8").PutValue(2021);
cells.Get(u"C8").PutValue(700);
cells.Get(u"A9").PutValue(u"Cherry");
cells.Get(u"B9").PutValue(2021);
cells.Get(u"C9").PutValue(800);
cells.Get(u"A10").PutValue(u"Grape");
cells.Get(u"B10").PutValue(2021);
cells.Get(u"C10").PutValue(900);
// Add pivot table sourced from A1:C10, anchored at E3, named "Pivot1"
int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
// Step 1: register a new custom pivot table style and capture its index
int styleIndex = workbook.GetWorksheets().GetTableStyles().AddPivotTableStyle(u"CustomPivotStyle");
TableStyle tableStyle = workbook.GetWorksheets().GetTableStyles().Get(styleIndex);
// Step 2: add a WholeTable element and apply thin black borders on all four sides
int wholeTableElementIndex = tableStyle.GetTableStyleElements().Add(TableStyleElementType::WholeTable);
TableStyleElement wholeTableElement = tableStyle.GetTableStyleElements().Get(wholeTableElementIndex);
Style wholeTableStyle = workbook.CreateStyle();
wholeTableStyle.GetBorders().Get(BorderType::TopBorder).SetLineStyle(CellBorderType::Thin);
wholeTableStyle.GetBorders().Get(BorderType::TopBorder).SetColor(Color::Black());
wholeTableStyle.GetBorders().Get(BorderType::BottomBorder).SetLineStyle(CellBorderType::Thin);
wholeTableStyle.GetBorders().Get(BorderType::BottomBorder).SetColor(Color::Black());
wholeTableStyle.GetBorders().Get(BorderType::LeftBorder).SetLineStyle(CellBorderType::Thin);
wholeTableStyle.GetBorders().Get(BorderType::LeftBorder).SetColor(Color::Black());
wholeTableStyle.GetBorders().Get(BorderType::RightBorder).SetLineStyle(CellBorderType::Thin);
wholeTableStyle.GetBorders().Get(BorderType::RightBorder).SetColor(Color::Black());
wholeTableElement.SetElementStyle(wholeTableStyle);
// Step 3: add a GrandTotalRow element and apply bold red font
int grandTotalElementIndex = tableStyle.GetTableStyleElements().Add(TableStyleElementType::GrandTotalRow);
TableStyleElement grandTotalElement = tableStyle.GetTableStyleElements().Get(grandTotalElementIndex);
Style grandTotalStyle = workbook.CreateStyle();
grandTotalStyle.GetFont().SetIsBold(true);
grandTotalStyle.GetFont().SetColor(Color::Red());
grandTotalElement.SetElementStyle(grandTotalStyle);
// Step 4: apply the custom style by name (NOT by PivotTableStyleType, which is for built-in presets)
pivotTable.SetPivotTableStyleName(u"CustomPivotStyle");
workbook.Save(u"output.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
PivotTable.FormatAll(Style) is a shortcut that applies a single Style object to every cell of the pivot table, including the data area, row and column headers, and totals. Whatever was previously set through PivotTableStyleType or PivotTableStyleName is overridden.
FormatAll overrides both PivotTableStyleType and PivotTableStyleName. Use it only when a uniform, theme-independent appearance is required across the entire pivot.
The following example creates a Style with a yellow solid fill, a bold dark-blue font, and thin black borders on all sides, then applies it with FormatAll and saves as .xlsx.
#include "Aspose.Cells.h"
#include <string>
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook wb;
Worksheet worksheet = wb.GetWorksheets().Get(0);
// Header row
worksheet.GetCells().Get(u"A1").PutValue(u"Fruit");
worksheet.GetCells().Get(u"B1").PutValue(u"Year");
worksheet.GetCells().Get(u"C1").PutValue(u"Amount");
// Data rows
worksheet.GetCells().Get(u"A2").PutValue(u"Grape");
worksheet.GetCells().Get(u"B2").PutValue(2020);
worksheet.GetCells().Get(u"C2").PutValue(5000);
worksheet.GetCells().Get(u"A3").PutValue(u"Blueberry");
worksheet.GetCells().Get(u"B3").PutValue(2020);
worksheet.GetCells().Get(u"C3").PutValue(3000);
worksheet.GetCells().Get(u"A4").PutValue(u"Kiwi");
worksheet.GetCells().Get(u"B4").PutValue(2020);
worksheet.GetCells().Get(u"C4").PutValue(4000);
worksheet.GetCells().Get(u"A5").PutValue(u"Cherry");
worksheet.GetCells().Get(u"B5").PutValue(2020);
worksheet.GetCells().Get(u"C5").PutValue(2000);
worksheet.GetCells().Get(u"A6").PutValue(u"Grape");
worksheet.GetCells().Get(u"B6").PutValue(2021);
worksheet.GetCells().Get(u"C6").PutValue(6000);
worksheet.GetCells().Get(u"A7").PutValue(u"Blueberry");
worksheet.GetCells().Get(u"B7").PutValue(2021);
worksheet.GetCells().Get(u"C7").PutValue(3500);
worksheet.GetCells().Get(u"A8").PutValue(u"Kiwi");
worksheet.GetCells().Get(u"B8").PutValue(2021);
worksheet.GetCells().Get(u"C8").PutValue(4500);
worksheet.GetCells().Get(u"A9").PutValue(u"Cherry");
worksheet.GetCells().Get(u"B9").PutValue(2021);
worksheet.GetCells().Get(u"C9").PutValue(2500);
worksheet.GetCells().Get(u"A10").PutValue(u"Grape");
worksheet.GetCells().Get(u"B10").PutValue(2021);
worksheet.GetCells().Get(u"C10").PutValue(5500);
// Add pivot table: source range A1:C10, destination cell E3, name "Pivot1"
int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
// Assign pivot fields
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
// Build a Style that will be forced onto every cell of the pivot table
Style style = wb.CreateStyle();
style.SetForegroundColor(Color::Yellow());
style.SetPattern(BackgroundType::Solid);
style.GetFont().SetIsBold(true);
style.GetFont().SetColor(Color::DarkBlue());
style.GetBorders().Get(BorderType::TopBorder).SetLineStyle(CellBorderType::Thin);
style.GetBorders().Get(BorderType::TopBorder).SetColor(Color::Black());
style.GetBorders().Get(BorderType::BottomBorder).SetLineStyle(CellBorderType::Thin);
style.GetBorders().Get(BorderType::BottomBorder).SetColor(Color::Black());
style.GetBorders().Get(BorderType::LeftBorder).SetLineStyle(CellBorderType::Thin);
style.GetBorders().Get(BorderType::LeftBorder).SetColor(Color::Black());
style.GetBorders().Get(BorderType::RightBorder).SetLineStyle(CellBorderType::Thin);
style.GetBorders().Get(BorderType::RightBorder).SetColor(Color::Black());
// Apply FormatAll
pivotTable.FormatAll(style);
// Save the workbook
wb.Save(u"output.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
The choice of style API depends on the file format you are saving to. Use the table below as a quick reference.
| Target file format | API to use | Notes |
|---|---|---|
.xls (legacy) |
PivotTable.AutoFormatType |
Values from Aspose.Cells.Pivot.PivotTableAutoFormatType (e.g. Report1–Report10, Classic, Table1–Table10). Ignored when saving as modern formats. |
.xlsx / .xlsm / .xlsb (modern, built-in style) |
PivotTable.PivotTableStyleType |
Values from Aspose.Cells.PivotTableStyleType (light/dark themes, including Excel 2017 additions). |
.xlsx / .xlsm / .xlsb (modern, custom style) |
PivotTable.PivotTableStyleName + Worksheets.TableStyles.AddPivotTableStyle(...) |
Use when the built-in presets are not enough. Configure via TableStyleElement.SetElementStyle(...). |
| Any format (uniform override) | PivotTable.FormatAll(Style) |
Shortcut that overrides every other style setting across the entire pivot. |
When in doubt, save as .xlsx and use PivotTableStyleType for built-in themes, or PivotTableStyleName for custom themes.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.