访问和更新单元格的富文本部分
Contents
[
Hide
]
Aspose.Cells允许您访问和更新单元格的富文本的部分。为此,您可以使用Cell.GetCharacters()和Cell.SetCharacters()方法。这些方法将返回并接受FontSetting对象数组,您可以使用它们来访问和更新字体的各种属性,如字体名称、字体颜色、粗体等。
访问和更新单元格的富文本部分
以下代码演示了如何使用Cell.GetCharacters()和Cell.SetCharacters()方法,该方法使用source excel file,您可以从提供的链接中下载。源Excel文件在单元格A1中有富文本。它有3个部分,每个部分都有不同的字体。以下代码片段访问这些部分,并使用新的字体名称更新第一个部分。最后,它将工作簿另存为output excel file。当您打开它时,您会发现文本的第一个部分的字体已更改为"Arial"。
###访问和更新单元格的富文本部分的C#代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
样本代码生成的控制台输出
以下是使用source excel file的上述示例代码的控制台输出。
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana