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

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

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

// 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