Formater le tableau croisé dynamique Cells
Parfois, vous souhaitez formater des cellules de tableau croisé dynamique. Par exemple, vous souhaitez appliquer une couleur d’arrière-plan aux cellules d’un tableau croisé dynamique. Aspose.Cells fournit deux méthodesTableau croisé dynamique.formatAll() etTableau croisé dynamique.format(), que vous pouvez utiliser à cette fin.
Tableau croisé dynamique.formatAll() applique le style à l’ensemble du tableau croisé dynamique tandis queTableau croisé dynamique.format() applique le style à une seule cellule du tableau croisé dynamique.
L’exemple de code suivant met en forme l’intégralité du tableau croisé dynamique avec une couleur bleu clair, puis met en forme la deuxième ligne du tableau en jaune.
Le tableau croisé dynamique d’entrée, avant l’exécution du code
Le tableau croisé dynamique de sortie, après l’exécution du code
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() |