访问和更新 Cell 的富文本部分
Contents
[
Hide
]
Aspose.Cells 允许您访问和更新单元格的富文本部分。为此,您可以使用 Cell.getCharacters() 和 Cell.setCharacters() 方法。这些方法将返回并接受 FontSetting 对象数组,您可以使用这些对象来访问和更新字体的各种属性,如字体名称、字体颜色、粗体等。
访问和更新 Cell 的富文本部分
以下代码演示了使用 Cell.getCharacters() 和 Cell.setCharacters() 方法的用法源文件您可以从提供的链接下载。源 excel 文件在单元格 A1 中包含富文本。它有 3 个部分,每个部分都有不同的字体。我们将访问这些部分并使用新字体名称更新第一部分。最后它将工作簿另存为输出excel文件.当你打开它时,你会发现第一部分文字的字体已经变成了**“宋体”**.
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-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"); |
控制台输出
这是上面示例代码的控制台输出,使用源文件.
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana