PivotTableのPivotFieldのDisplayNameでCellオブジェクトを取得
Contents
[
Hide
]
Aspose.Cells提供PivotTable.GetCellByDisplayName()ピボット フィールドの表示名でセル オブジェクトにアクセスするために使用できるメソッド。このメソッドは、ピボット フィールド ヘッダーを強調表示または書式設定する場合に便利です。この記事では、データ フィールドの表示名でセル オブジェクトを取得し、それに書式を適用する方法について説明します。
PivotTableのPivotFieldのDisplayNameでCellオブジェクトを取得
次のコードは、ワークシートの最初のピボット テーブルにアクセスし、ピボット テーブルの 2 番目のデータ フィールドの表示名でセルを取得します。次に、セルの塗りつぶしの色とフォントの色をそれぞれ水色と黒に変更します。以下のスクリーンショットは、コードの実行前後でピボット テーブルがどのように見えるかを示しています。
ピボット テーブル - 変更前 |
---|
![]() |
This file contains hidden or 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object from source excel file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access first pivot table inside the worksheet | |
PivotTable pivotTable = worksheet.PivotTables[0]; | |
// Access cell by display name of 2nd data field of the pivot table | |
Cell cell = pivotTable.GetCellByDisplayName(pivotTable.DataFields[0].DisplayName); | |
// Access cell style and set its fill color and font color | |
Style style = cell.GetStyle(); | |
style.ForegroundColor = Color.LightBlue; | |
style.Font.Color = Color.Black; | |
// Set the style of the cell | |
pivotTable.Format(cell.Row, cell.Column, style); | |
// Save workbook | |
workbook.Save(dataDir + "output_out.xlsx"); |
ピボットテーブル - 後 |
---|
![]() |