Obtenga el objeto Cell por DisplayName de PivotField de PivotTable
Contents
[
Hide
]
Aspose.Cells proporciona[Tabla dinámica.getCellByDisplayName()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#getCellByDisplayName(java.lang.String)método que puede usar para acceder al objeto de la celda por el nombre para mostrar del campo dinámico. Este método es útil cuando desea resaltar o formatear el encabezado de su campo dinámico. Este artículo explica cómo recuperar el objeto de celda por el nombre para mostrar del campo de datos y luego aplicarle formato.
Obtenga el objeto Cell por DisplayName de PivotField de PivotTable
El siguiente código accede a la primera tabla dinámica de la hoja de trabajo y luego obtiene la celda por el nombre para mostrar del segundo campo de datos de la tabla dinámica. Luego cambia el color de relleno y el color de fuente de la celda a azul claro y negro respectivamente. Las siguientes capturas de pantalla muestran cómo se ve la tabla dinámica antes y después de la ejecución del código.
Tabla dinámica: antes
Tabla dinámica - Después
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
// 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"); |