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