格式化数据透视表单元格
Contents
[
Hide
]
有时,您可能希望格式化数据透视表单元格。例如,您可能希望对数据透视表单元格应用背景颜色。Aspose.Cells提供了两种方法PivotTable.FormatAll()和PivotTable.Format(),您可以用于此目的。
PivotTable.FormatAll() 将样式应用到整个数据透视表,而 PivotTable.Format() 将样式应用到数据透视表的单个单元格。
以下示例代码加载了包含两个数据透视表的 示例 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"); |