Format Pivot Table Cells
Sometimes, you want to format pivot table cells. For example, you want to apply background color to pivot table cells. Aspose.Cells provides two methods PivotTable.FormatAll() and PivotTable.Format(), which you can use for this purpose.
PivotTable.FormatAll() applies the style to entire pivot table while PivotTable.Format() applies the style to a single cell of the pivot table.
The following sample code loads the sample Excel file that contains two pivot tables, and achieve the operation of formatting the entire pivot table and formatting individual cells in the pivot table.
// Create workbook object from source file containing pivot table | |
Workbook workbook = new Workbook("pivot_format.xlsx"); | |
// Access the worksheet by its name | |
Worksheet worksheet = workbook.Worksheets["Sheet1"]; | |
// Access the pivot table | |
PivotTable pivotTable = worksheet.PivotTables[1]; | |
// Create a style object with background color light blue | |
Style style = workbook.CreateStyle(); | |
style.Pattern = BackgroundType.Solid; | |
style.BackgroundColor = Color.LightBlue; | |
// Format entire pivot table with light blue color | |
pivotTable.FormatAll(style); | |
// Create another style object with yellow color | |
style = workbook.CreateStyle(); | |
style.Pattern = BackgroundType.Solid; | |
style.BackgroundColor = Color.Yellow; | |
// Access the pivot table | |
PivotTable pivotTable2 = worksheet.PivotTables[0]; | |
// Format the cell of pivot table | |
pivotTable2.Format(16, 5, style); | |
// Save the workbook object | |
workbook.Save("out.xlsx"); |