格式化数据透视表 Cells
Contents
[
Hide
]
有时,您想要格式化数据透视表单元格。例如,您想要将背景颜色应用于数据透视表单元格。 Aspose.Cells提供两种方法[数据透视表.formatAll()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#formatAll(com.aspose.cells.Style) ) 和数据透视表.format(),您可以将其用于此目的。
数据透视表.formatAll() 将样式应用于整个数据透视表,同时[数据透视表.format()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#format(int,%20int,%20com.aspose.cells.Style)将样式应用于数据透视表的单个单元格。
以下示例代码将整个数据透视表格式化为浅蓝色,然后将第二个表格行格式化为黄色。
输入数据透视表,在执行代码之前
执行代码后的输出数据透视表
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FormatPivotTableCells.class); | |
// Create workbook object from source file containing pivot table | |
Workbook workbook = new Workbook(dataDir + "pivotTable_test.xlsx"); | |
// Access the worksheet by its name | |
Worksheet worksheet = workbook.getWorksheets().get("PivotTable"); | |
// Access the pivot table | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Create a style object with background color light blue | |
Style 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 | |
for (int col = 0; col < 5; col++) { | |
pivotTable.format(1, col, style); | |
} | |
// Save the workbook object | |
workbook.save(dataDir + "out.xlsx"); |