Formatter les cellules du tableau croisé dynamique
Parfois, vous souhaitez formater les cellules de tableau croisé dynamique. Par exemple, vous souhaitez appliquer une couleur de fond aux cellules de tableau croisé dynamique. Aspose.Cells propose deux méthodes PivotTable.formatAll() et PivotTable.format(), que vous pouvez utiliser à cet effet.
PivotTable.formatAll() applique le style à l’ensemble du tableau croisé dynamique tandis que PivotTable.format() applique le style à une seule cellule du tableau croisé dynamique.
Le code d’exemple suivant formate l’ensemble du tableau croisé dynamique avec une couleur bleu clair, puis formate 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FormatPivotTableCells.class); | |
// Create workbook object from source file containing pivot table | |
Workbook workbook = new Workbook(dataDir + "pivotTable_test.xlsx"); | |
// Access the worksheet by its name | |
Worksheet worksheet = workbook.getWorksheets().get("PivotTable"); | |
// Access the pivot table | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Create a style object with background color light blue | |
Style 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 | |
for (int col = 0; col < 5; col++) { | |
pivotTable.format(1, col, style); | |
} | |
// Save the workbook object | |
workbook.save(dataDir + "out.xlsx"); |