Accedere e aggiornare le porzioni di testo arricchito della cella

Accedere e aggiornare le porzioni di testo arricchito della cella

Il seguente codice dimostra l’utilizzo dei metodi Cell.getCharacters() e Cell.setCharacters() utilizzando il file excel di origine che puoi scaricare dal link fornito. Il file excel di origine ha un testo arricchito nella cella A1. Ha 3 porzioni e ciascuna porzione ha un font diverso. Accederemo a queste porzioni e aggiorneremo la prima porzione con il nuovo nome del font. Infine salva il foglio di lavoro come file excel di output. Quando lo aprirai, troverai che il font della prima porzione del testo è stato cambiato in “Arial”.

// 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(AccessAndUpdatePortions.class);
Workbook workbook = new Workbook(dataDir + "source.xlsx");
Worksheet worksheet = workbook.getWorksheets().get(0);
Cell cell = worksheet.getCells().get("A1");
System.out.println("Before updating the font settings....");
FontSetting[] fnts = cell.getCharacters();
for (int i = 0; i < fnts.length; i++) {
System.out.println(fnts[i].getFont().getName());
}
// Modify the first FontSetting Font Name
fnts[0].getFont().setName("Arial");
// And update it using SetCharacters() method
cell.setCharacters(fnts);
System.out.println();
System.out.println("After updating the font settings....");
fnts = cell.getCharacters();
for (int i = 0; i < fnts.length; i++) {
System.out.println(fnts[i].getFont().getName());
}
// Save workbook
workbook.save(dataDir + "output.xlsx");

Output della console

Ecco l’output della console del codice di esempio usando il file excel di origine.

 Before updating the font settings....

Century

Courier New

Verdana

After updating the font settings....

Arial

Courier New

Verdana