Acceder y Actualizar las Partes de Texto Enriquecido de la Celda
Acceder y actualizar partes de texto enriquecido de la celda
El siguiente código demuestra el uso de los métodos Cell.getCharacters() y Cell.setCharacters() utilizando el archivo excel fuente que puedes descargar desde el enlace proporcionado. El archivo excel fuente tiene un texto con formato rico en la celda A1. Tiene 3 porciones y cada porción tiene una fuente diferente. Accederemos a estas porciones y actualizaremos la primera porción con un nuevo nombre de fuente. Finalmente guarda el libro como archivo excel de salida. Cuando lo abras, verás que la fuente de la primera porción del texto ha cambiado a “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"); |
Salida de la consola
Aquí está la salida en consola del código de ejemplo anterior usando el archivo excel fuente.
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana