Documents for Excel, Java Edition Documentation
Features / Worksheet / Rich Text
In This Topic
    Rich Text
    In This Topic

    GcExcel.NET provides support for applying rich text formatting in the cells of the worksheet. By default, when textual information is entered in a cell, the alphabets are displayed without any formatting style. Rich text feature allows you to apply multiple styles to the text by highlighting important characters or alphabets using different colors, font family, font effects (bold, underline, double underline, strikethrough, subscript, superscript) and font size etc.

    Let's say you are working in a spreadsheet where the cells contain some characters that need to be highlighted to a greater extent in order to put emphasis on crucial information like the name of an organization, the flagship product of the company, a number, or any other sensitive data. In such a scenario, rich text feature comes in handy while setting multiple styles in a cell.

    In the following example, cell A1 contains a string where rich text formatting has been applied. The word "Documents" is formatted with a custom font size, underline style and blue color. Similarly, the text "GrapeCity" and "Excel" has been formatted using multiple styles.

     

    Rich text

     

    Users can use any of the following ways in order to set the rich text in the cells of a worksheet -

    Using the IRichText Interface.

    The add method of the IRichText interface can be used to add specific ranges of text to the RichText collection of IText runs.

    Using Code

    Refer to the following example code in order to set rich text in the cells of a worksheet using the IRichText interface.

    Java
    Copy Code
    // Setting column "A" width
    worksheet.getRange("A1").setColumnWidth(70);
    
    // Using IRichText interface to add rich text in cell range A1
    
    // Fetch the IRichText object associated with the cell range
    IRichText richText = worksheet.getRange("A1").getRichText();
    
    // Add string "GrapeCity " to IRichText object and apply formatting
    ITextRun run1 = richText.add("GrapeCity ");
    run1.getFont().setColor(Color.GetRed());
    run1.getFont().setBold(true);
    run1.getFont().setSize(20);
    
    // Append string "Documents" to IRichText object and apply formatting
    ITextRun run2 = richText.add("Documents");
    run2.getFont().setThemeFont(ThemeFont.Major);
    run2.getFont().setThemeColor(ThemeColor.Accent1);
    run2.getFont().setSize(30);
    run2.getFont().setUnderline(UnderlineType.Single);
    
    // Append string " for " to IRichText object
    richText.add(" for ");
    
    // Append string "Excel" to IRichText object and apply formatting
    ITextRun run3 = richText.add("Excel");
    run3.getFont().setName("Arial Black");
    run3.getFont().setColor(Color.GetLightGreen());
    run3.getFont().setSize(36);
    run3.getFont().setItalic(true);
    

    Using the IRange.characters()

    The characters() method of the IRange interface can be used to represent a range of characters within the text entered in the cell. This method can be called only when the cell value is entered in the string format.  

    Using Code

    Refer to the following example code in order to set rich text in the cells of a worksheet.

    Java
    Copy Code
            
    // Setting column "A" width
    worksheet.getRange("A1").setColumnWidth(70);
    
    // Use IRange.Characters() to add rich text
    
    // Setting Cell Text
    worksheet.getRange("A1").setValue("GrapeCity Documents for Excel");
    
    // Extracting character ranges from cell text and applying different formatting rules to each range
    
    // Formatting string "Grapecity"
    ITextRun run1 = worksheet.getRange("A1").characters(0, 9);
    run1.getFont().setColor(Color.GetRed());
    run1.getFont().setBold(true);
    run1.getFont().setSize(20);
    
    // Formatting string "Documents"
    ITextRun run2 = worksheet.getRange("A1").characters(10, 9);
    run2.getFont().setThemeFont(ThemeFont.Major);
    run2.getFont().setThemeColor(ThemeColor.Accent1);
    run2.getFont().setSize(30);
    run2.getFont().setUnderline(UnderlineType.Single);
    
    // Formatting string "Excel"
    ITextRun run3 = worksheet.getRange("A1").characters(24, 5);
    run3.getFont().setName("Arial Black");
    run3.getFont().setColor(Color.GetLightGreen());
    run3.getFont().setSize(36);
    run3.getFont().setItalic(true);
    

    Using the IRange.characters() to Configure Font Across Several Runs

    You can also insert rich text in the cells of a worksheet by using the characters() method of the IRange interface. Using this method, you can configure the font across several runs and then consolidate them into a single entity.

    Using Code

    Refer to the following example code in order to set rich text in the cells of a worksheet.

    Java
    Copy Code
            
    // Setting column "A" width
    worksheet.getRange("A1").setColumnWidth(75);
    
    // Use IRange.Characters() to config font across several runs
    
    // Fetch the IRichText object associated with the cell range
    IRichText richText = worksheet.getRange("A1").getRichText();
    
    // Add string "GrapeCity " to IRichText object and apply formatting
    ITextRun run1 = richText.add("GrapeCity ");
    run1.getFont().setColor(Color.GetRed());
    run1.getFont().setBold(true);
    run1.getFont().setSize(20);
    
    // Append string "Documents" to IRichText object and apply formatting
    ITextRun run2 = richText.add("Documents");
    run2.getFont().setThemeFont(ThemeFont.Major);
    run2.getFont().setThemeColor(ThemeColor.Accent1);
    run2.getFont().setSize(30);
    run2.getFont().setUnderline(UnderlineType.Single);
    
    // Append string " for " to IRichText object
    richText.add(" for ");
    
    // Append string "Excel" to IRichText object and apply formatting
    ITextRun run3 = richText.add("Excel");
    run3.getFont().setName("Arial Black");
    run3.getFont().setColor(Color.GetLightGreen());
    run3.getFont().setSize(36);
    run3.getFont().setItalic(true);
    
    // Create composite run
    // Extract character range composed of "City" word from run1 and " for" word and apply formatting
    ITextRun compositeRun = worksheet.getRange("A1").characters(5, 18);
    compositeRun.getFont().setBold(true);
    compositeRun.getFont().setItalic(true);
    compositeRun.getFont().setThemeColor(ThemeColor.Accent1);
            

    Using the ITextRun.insertAfter() and ITextRun.insertBefore()

    The ITextRun interface provides the properties and methods for adding and customizing the rich text entered in the cells of the worksheet. The insertAfter() and insertBefore() methods of the ITextRun interface can be used to insert rich text after and before a range of characters respectively. Also, you can use the delete() method of the ITextRun interface in order to delete the inserted rich text in the cells.

    Using Code

    Refer to the following example code in order to set rich text in the cells of a worksheet.

    Java
    Copy Code
            
    // Setting column "A" width
    worksheet.getRange("A1").setColumnWidth(70);
    
    // Use ITextRun.insertAfter() and insertBefore() to add rich text
    
    // Fetch the IRichText object associated with the cell range
    IRichText richText = worksheet.getRange("A1").getRichText();
    
    // Add string " for " to IRichText object
    ITextRun run1 = richText.add(" for ");
    
    // Use InsertBefore() to add string "Documents" to run1 and apply formatting
    ITextRun run2 = run1.insertBefore("Documents");
    run2.getFont().setThemeFont(ThemeFont.Major);
    run2.getFont().setThemeColor(ThemeColor.Accent1);
    run2.getFont().setSize(30);
    run2.getFont().setUnderline(UnderlineType.Single);
    
    // Use InsertBefore() to add string "GrapeCity " to run2 and apply formatting
    ITextRun run3 = run2.insertBefore("GrapeCity ");
    run3.getFont().setColor(Color.GetRed());
    run3.getFont().setBold(true);
    run3.getFont().setSize(20);
    
    // Use InsertAfter() to add string "Excel" to run1 and apply formatting
    ITextRun run4 = run1.insertAfter("Excel");
    run4.getFont().setName("Arial Black");
    run4.getFont().setColor(Color.GetLightGreen());
    run4.getFont().setSize(36);
    run4.getFont().setItalic(true);