Ottieni l'oggetto Cell da DisplayName di PivotField di PivotTable

Ottieni l’oggetto Cell da DisplayName di PivotField di PivotTable

Il codice seguente accede alla prima tabella pivot del foglio di lavoro e quindi ottiene cell in base al nome visualizzato del secondo campo dati della tabella pivot. Quindi cambia il colore di riempimento e il colore del carattere della cella rispettivamente in azzurro e nero. Gli screenshot seguenti mostrano l’aspetto della tabella pivot prima e dopo l’esecuzione del codice.

Tabella pivot - Prima

cose da fare:immagine_alt_testo

Tabella pivot - Dopo

cose da fare:immagine_alt_testo

// 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");