PivotTableのPivotFieldのDisplayNameによってCellオブジェクトを取得する
Contents
[
Hide
]
Aspose.Cellsは、PivotTable.getCellByDisplayName()メソッドを提供し、ピボットテーブルの表示名によるセルオブジェクトへのアクセスに使用できます。このメソッドは、ピボットフィールドヘッダーを強調表示または書式設定する場合に有用です。この記事では、データフィールドの表示名によるセルオブジェクトの取得とその後の書式設定方法を説明しています。
PivotTableのPivotFieldのDisplayNameによってCellオブジェクトを取得する
次のコードは、ワークシートの最初のピボットテーブルにアクセスし、その後、ピボットテーブルの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(GetCellObject.class); | |
// Create workbook object from source excel file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access first pivot table inside the worksheet | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Access cell by display name of 2nd data field of the pivot table | |
String displayName = pivotTable.getDataFields().get(1).getDisplayName(); | |
Cell cell = pivotTable.getCellByDisplayName(displayName); | |
// Access cell style and set its fill color and font color | |
Style style = cell.getStyle(); | |
style.setForegroundColor(Color.getLightBlue()); | |
style.getFont().setColor(Color.getBlack()); | |
// Set the style of the cell | |
pivotTable.format(cell.getRow(), cell.getColumn(), style); | |
// Save workbook | |
workbook.save(dataDir + "output.xlsx"); |