访问和更新 Cell 的富文本部分

访问和更新 Cell 的富文本部分

下面的代码演示了使用Cell.GetCharacters()Cell.SetCharacters()方法使用源文件您可以从提供的链接下载。源 excel 文件在单元格 A1 中包含富文本。它有 3 个部分,每个部分都有不同的字体。以下代码片段访问这些部分并使用新字体名称更新第一部分。最后,它将工作簿另存为输出excel文件.当你打开它时,你会发现第一部分文字的字体已经变成了**“宋体”**.

C# 访问和更新 Cell 富文本部分的代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string inputPath = dataDir + "Sample.xlsx";
string outputPath = dataDir + "Output.out.xlsx";
Workbook workbook = new Workbook(inputPath);
Worksheet worksheet = workbook.Worksheets[0];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine("Before updating the font settings....");
FontSetting[] fnts = cell.GetCharacters();
for (int i = 0; i < fnts.Length; i++)
{
Console.WriteLine(fnts[i].Font.Name);
}
// Modify the first FontSetting Font Name
fnts[0].Font.Name = "Arial";
// And update it using SetCharacters() method
cell.SetCharacters(fnts);
Console.WriteLine();
Console.WriteLine("After updating the font settings....");
fnts = cell.GetCharacters();
for (int i = 0; i < fnts.Length; i++)
{
Console.WriteLine(fnts[i].Font.Name);
}
// Save workbook
workbook.Save(outputPath);

示例代码生成的控制台输出

这是上面示例代码的控制台输出,使用源文件.

Before updating the font settings....

Century

Courier New

Verdana

After updating the font settings....

Arial

Courier New

Verdana