Formato de celdas de tabla dinámica
A veces desearás dar formato a las celdas de una tabla dinámica. Por ejemplo, aplicar un color de fondo a las celdas de una tabla dinámica. Aspose.Cells proporciona dos métodos PivotTable.formatAll() y PivotTable.format(), que puedes utilizar para este propósito.
PivotTable.formatAll() aplica el estilo a toda la tabla dinámica, mientras que PivotTable.format() aplica el estilo a una única celda de la tabla dinámica.
El siguiente código de ejemplo da formato a toda la tabla dinámica con un color azul claro y luego formatea la segunda fila de la tabla con color amarillo.
La tabla dinámica de entrada, antes de ejecutar el código
La tabla dinámica de salida, después de ejecutar el código
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import Workbook, SaveFormat | |
# Create workbook object from source file containing pivot table | |
workbook = Workbook("pivotTable_test.xlsx") | |
# Access the worksheet by its name | |
worksheet = workbook.getWorksheets().get("PivotTable") | |
# Access the pivot table | |
pivotTable = worksheet.getPivotTables().get(0) | |
# Create a style object with background color light blue | |
style = workbook.createStyle() | |
style.setPattern(BackgroundType.SOLID) | |
style.setBackgroundColor(Color.getLightBlue()) | |
# Format entire pivot table with light blue color | |
pivotTable.formatAll(style) | |
# Create another style object with yellow color | |
style = workbook.createStyle() | |
style.setPattern(BackgroundType.SOLID) | |
style.setBackgroundColor(Color.getYellow()) | |
# Format the cells of the first row of the pivot table with yellow color | |
columns = [0, 1, 2, 3, 4] | |
for x in columns: | |
pivotTable.format(1, x, style) | |
# Save the workbook object | |
workbook.save("out.xlsx") | |
jpype.shutdownJVM() |