Formatieren von Pivottabellenzellen
Manchmal möchten Sie Pivot-Tabelle Zellen formatieren. Zum Beispiel möchten Sie eine Hintergrundfarbe auf Pivot-Tabelle Zellen anwenden. Aspose.Cells bietet zwei Methoden PivotTable.formatAll() und PivotTable.format(), die Sie zu diesem Zweck verwenden können.
PivotTable.formatAll() wendet den Stil auf die gesamte Pivot-Tabelle an, während PivotTable.format() den Stil auf eine einzelne Zelle der Pivot-Tabelle anwendet.
Der folgende Beispielcode formatiert die gesamte Pivot-Tabelle mit einer hellblauen Farbe und formatiert dann die zweite Tabellenzeile gelb.
Der Eingabe-Pivot-Table, bevor der Code ausgeführt wird
Der Ausgabe-Pivot-Table, nach Ausführung des Codes
// 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"); |