ピボットテーブルセルの書式設定
Contents
[
Hide
]
時々、ピボットテーブルセルの書式を設定したいことがあります。たとえば、ピボットテーブルセルに背景色を適用したい場合があります。Aspose.Cells では、この目的で使用できる2つの方法 PivotTable.FormatAll() および PivotTable.Format() を提供しています。
PivotTable.FormatAll() は、ピボットテーブル全体にスタイルを適用し、PivotTable.Format() はピボットテーブルの個々のセルにスタイルを適用します。
次のサンプルコードは、2つのピボットテーブルを含む サンプルExcelファイル を読み込み、全体のピボットテーブルの書式設定およびピボットテーブル内の個々のセルの書式設定の操作を実行します。
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
// Create workbook object from source file containing pivot table | |
Workbook workbook = new Workbook("pivot_format.xlsx"); | |
// Access the worksheet by its name | |
Worksheet worksheet = workbook.Worksheets["Sheet1"]; | |
// Access the pivot table | |
PivotTable pivotTable = worksheet.PivotTables[1]; | |
// Create a style object with background color light blue | |
Style style = workbook.CreateStyle(); | |
style.Pattern = BackgroundType.Solid; | |
style.BackgroundColor = Color.LightBlue; | |
// Format entire pivot table with light blue color | |
pivotTable.FormatAll(style); | |
// Create another style object with yellow color | |
style = workbook.CreateStyle(); | |
style.Pattern = BackgroundType.Solid; | |
style.BackgroundColor = Color.Yellow; | |
// Access the pivot table | |
PivotTable pivotTable2 = worksheet.PivotTables[0]; | |
// Format the cell of pivot table | |
pivotTable2.Format(16, 5, style); | |
// Save the workbook object | |
workbook.Save("out.xlsx"); |