Formattare le celle della tabella pivot
A volte vuoi formattare le celle della tabella pivot. Ad esempio, vuoi applicare un colore di sfondo alle celle della tabella pivot. Aspose.Cells fornisce due metodi PivotTable.FormatAll() e PivotTable.Format(), che puoi utilizzare a questo scopo.
PivotTable.FormatAll() applica lo stile all’intera tabella pivot mentre PivotTable.Format() applica lo stile a una singola cella della tabella pivot.
Il seguente esempio di codice carica il file Excel di esempio che contiene due tabelle pivot e raggiunge l’operazione di formattare l’intera tabella pivot e formattare le singole celle nella tabella pivot.
// 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"); |