通过PivotTable的PivotField的DisplayName获取Cell对象
Contents
[
Hide
]
Aspose.Cells提供[数据透视表.getCellByDisplayName()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#getCellByDisplayName(java.lang.String)方法,您可以使用该方法通过数据透视字段的显示名称访问单元格对象。当您想要突出显示或格式化数据透视字段标题时,此方法很有用。本文介绍如何通过数据字段的显示名称检索单元格对象,然后对其应用格式。
通过PivotTable的PivotField的DisplayName获取Cell对象
以下代码访问工作表的第一个数据透视表,然后通过数据透视表的第二个数据字段的显示名称获取单元格。然后它将单元格的填充颜色和字体颜色分别更改为浅蓝色和黑色。下面的屏幕截图显示了代码执行前后数据透视表的外观。
数据透视表 - 之前
数据透视表 - 之后
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(GetCellObject.class); | |
// Create workbook object from source excel file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access first pivot table inside the worksheet | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Access cell by display name of 2nd data field of the pivot table | |
String displayName = pivotTable.getDataFields().get(1).getDisplayName(); | |
Cell cell = pivotTable.getCellByDisplayName(displayName); | |
// Access cell style and set its fill color and font color | |
Style style = cell.getStyle(); | |
style.setForegroundColor(Color.getLightBlue()); | |
style.getFont().setColor(Color.getBlack()); | |
// Set the style of the cell | |
pivotTable.format(cell.getRow(), cell.getColumn(), style); | |
// Save workbook | |
workbook.save(dataDir + "output.xlsx"); |