Access and Update the Portions of Rich Text of Cell
Access and Update the Portions of Rich Text of Cell
The following code demonstrates the usage of Cell.getCharacters() and Cell.setCharacters() method using the source excel file which you can download from the provided link. The source excel file has a rich text in the cell A1. It has 3 portions and each portion has different font. We will access these portions and update the first portion with new font name. Finally it saves the workbook as output excel file. When you will open it, you will find the font of the first portion of the text has changed to “Arial”.
// 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"); |
Console Output
Here is the console output of the above sample code using the source excel file.
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana