访问和更新单元格的富文本部分

访问和更新单元格的富文本部分

以下代码演示了如何使用source excel file中的Cell.get_characters()Cell.set_characters()方法。您可以从提供的链接下载源Excel文件。源Excel文件中的单元格A1中有富文本。它有3个部分,每个部分都有不同的字体。以下代码段访问这些部分,并使用新的字体名称更新第一个部分。最后,它将工作簿另存为output excel file。当您打开它时,您会发现文本的第一部分的字体已经更改为**“Arial”**。

###访问和更新单元格的富文本部分的C#代码

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)

样本代码生成的控制台输出

以下是使用source excel file的上述示例代码的控制台输出。

Before updating the font settings....

Century

Courier New

Verdana

After updating the font settings....

Arial

Courier New

Verdana