Accedere e aggiornare le porzioni di testo arricchito della cella

Accedere e aggiornare le porzioni di testo arricchito della cella

Il codice seguente dimostra l’uso dei metodi Cell.get_characters() e Cell.set_characters() utilizzando il file excel di origine che è possibile scaricare dal link fornito. Il file excel di origine ha un testo arricchito nella cella A1. Ha 3 porzioni e ciascuna porzione ha un carattere diverso. Il seguente snippet di codice accede a queste porzioni e aggiorna la prima porzione con un nuovo nome di font. Infine, salva il workbook come file di output excel. Quando lo apri, troverai che il font della prima porzione del testo è stato cambiato in “Arial”.

Codice C# per accedere e aggiornare le porzioni di testo arricchito della cella

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
inputPath = dataDir + "Sample.xlsx"
outputPath = dataDir + "Output.out.xlsx"
workbook = Workbook(inputPath)
worksheet = workbook.worksheets[0]
cell = worksheet.cells.get("A1")
print("Before updating the font settings....")
fnts = cell.get_characters()
for i in range(len(fnts)):
print(fnts[i].font.name)
# Modify the first FontSetting Font Name
fnts[0].font.name = "Arial"
# And update it using SetCharacters() method
cell.set_characters(fnts)
print()
print("After updating the font settings....")
fnts = cell.get_characters()
for i in range(len(fnts)):
print(fnts[i].font.name)
# Save workbook
workbook.save(outputPath)

Uscita della console generata dal codice di esempio

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

Before updating the font settings....

Century

Courier New

Verdana

After updating the font settings....

Arial

Courier New

Verdana