Formato de celdas de tabla dinámica
A veces, desea dar formato a las celdas de tabla dinámica. Por ejemplo, desea aplicar color de fondo a las celdas de tabla dinámica. Aspose.Cells proporciona dos métodos PivotTable.FormatAll() y PivotTable.Format(), que puede utilizar con este propósito.
PivotTable.FormatAll() aplica el estilo a toda la tabla dinámica mientras que PivotTable.Format() aplica el estilo a una sola celda de la tabla dinámica.
El siguiente código de muestra carga el archivo de Excel de ejemplo que contiene dos tablas dinámicas y logra la operación de dar formato a toda la tabla dinámica y dar formato a las celdas individuales en la tabla dinámica.
// 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"); |