ピボットテーブルセルの書式設定
Contents
[
Hide
]
時々、ピボットテーブルセルのフォーマットが必要です。たとえば、ピボットテーブルセルに背景色を適用したい場合。Aspose.Cellsは、この目的に使用できる2つのメソッドPivotTable.formatAll()とPivotTable.format()を提供しています。
PivotTable.formatAll()はピボットテーブル全体にスタイルを適用し、PivotTable.format()はピボットテーブルの単一セルにスタイルを適用します。
以下のサンプルコードは、ピボットテーブル全体をライトブルーの色でフォーマットし、次に2番目の表行を黄色でフォーマットします。
コードを実行する前の入力ピボットテーブル
コードを実行した後の出力ピボットテーブル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |