格式化数据透视表单元格
Contents
[
Hide
]
有时,您想要格式化数据透视表单元。例如,您希望将背景颜色应用于数据透视表单元。Aspose.Cells提供了两个方法PivotTable.formatAll()和PivotTable.format(),您可以用于此目的。
PivotTable.formatAll()将样式应用于整个数据透视表,而PivotTable.format()将样式应用于数据透视表的单个单元格。
以下示例代码将整个数据透视表格式化为浅蓝色,然后将第二行表格式化为黄色。
执行代码前的输入数据透视表
执行代码后的输出数据透视表
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
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import Workbook, SaveFormat | |
# Create workbook object from source file containing pivot table | |
workbook = Workbook("pivotTable_test.xlsx") | |
# Access the worksheet by its name | |
worksheet = workbook.getWorksheets().get("PivotTable") | |
# Access the pivot table | |
pivotTable = worksheet.getPivotTables().get(0) | |
# Create a style object with background color light blue | |
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 | |
columns = [0, 1, 2, 3, 4] | |
for x in columns: | |
pivotTable.format(1, x, style) | |
# Save the workbook object | |
workbook.save("out.xlsx") | |
jpype.shutdownJVM() |