Excel cell inline text formatting

Using GemBox.Spreadsheet for Java you can apply font formatting (size, color, font type, italic and strikeout properties, different levels of boldness, underlining, subscript and superscript) to individual characters and words.

Following example shows how to format individual characters and words within a cell.

Excel cell inline text formatting set with GemBox.Spreadsheet for Java
Screenshot of Excel cell inline text formatting set with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using the Professional version, put your serial key below.

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Inline Text Formatting");

        worksheet.getCell(0, 0).setValue("Inline text formatting examples:");

        // Column width of 20 characters.
        worksheet.getColumn(0).setWidth(20 * 256);

        worksheet.getCell(2, 0).setValue("This is big and red text!");

        // Apply size to 'big and red' part of text
        worksheet.getCell(2, 0).getCharacters(8, 11).getFont().setSize(400);

        // Apply color to 'red' part of text
        worksheet.getCell(2, 0).getCharacters(16, 3).getFont().setColor(SpreadsheetColor.fromName(ColorName.RED));

        // Format cell content
        worksheet.getCell(4, 0).setValue("Formatting selected characters with GemBox.Spreadsheet for Java component.");
        worksheet.getCell(4, 0).getStyle().getFont().setColor(SpreadsheetColor.fromName(ColorName.BLUE));
        worksheet.getCell(4, 0).getStyle().getFont().setItalic(true);
        worksheet.getCell(4, 0).getStyle().setWrapText(true);

        // Get characters from index 36 to the end of string
        FormattedCharacterRange characters = worksheet.getCell(4, 0).getCharacters(36);

        // Apply color and underline to selected characters

        // Write selected characters
        worksheet.getCell(6, 0).setValue("Selected characters: " + characters.getText());

        workbook.save("Inline Text Formatting.%OutputFileType%");

See also

Next steps

GemBox.Spreadsheet for Java is a component that enables you to read, write, edit and convert spreadsheet files from your applications using one simple API.

Download Buy