[]
        
(Showing Draft Content)

Rich Text

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 have a worksheet wherein the cells contain some characters that need to be highlighted to a greater extent in order to emphasize on important information like the name of an organization, company's flagship product, 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

You can set the rich text in the cells of a worksheet by using any of the following ways -

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.

 
 // Setting column "A" width
 worksheet.Range["A1"].ColumnWidth = 70;

 // Using IRichText interface to add rich text in cell range A1 

 // Fetch the IRichText object associated with the cell range
 IRichText richText = worksheet.Range["A1"].RichText;

 // Add string "GrapeCity " to IRichText object and apply formatting
 ITextRun run1 = richText.Add("GrapeCity ");
 run1.Font.Color = Color.Red;
 run1.Font.Bold = true;
 run1.Font.Size = 20;

 // Append string "Documents" to IRichText object and apply formatting
 ITextRun run2 = richText.Add("Documents");
 run2.Font.ThemeFont = ThemeFont.Major;
 run2.Font.ThemeColor = ThemeColor.Accent1;
 run2.Font.Size = 30;
 run2.Font.Underline = 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.Font.Name = "Arial Black";
 run3.Font.Color = Color.LightGreen;
 run3.Font.Size = 36;
 run3.Font.Italic = 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 will be called only when the value of the cell is in the string format.

Using Code

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

// Setting column "A" width
worksheet.Range["A1"].ColumnWidth = 70;

// Use IRange.Characters() to add rich text

// Setting Cell Text
worksheet.Range["A1"].Value = "GrapeCity Documents for Excel";

// Extracting character ranges from cell text and applying different formatting rules to each range

// Formatting string "Grapecity"
ITextRun run1 = worksheet.Range["A1"].Characters(0, 9);
run1.Font.Color = Color.Red;
run1.Font.Bold = true;
run1.Font.Size = 20;

// Formatting string "Documents"
ITextRun run2 = worksheet.Range["A1"].Characters(10, 9);
run2.Font.ThemeFont = ThemeFont.Major;
run2.Font.ThemeColor = ThemeColor.Accent1;
run2.Font.Size = 30;
run2.Font.Underline = UnderlineType.Single;

// Formatting string "Excel"
ITextRun run3 = worksheet.Range["A1"].Characters(24, 5);
run3.Font.Name = "Arial Black";
run3.Font.Color = Color.LightGreen;
run3.Font.Size = 36;
run3.Font.Italic = true;
        

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

You can also insert rich text in the cells of a worksheet via using the Characters() method of the IRange interface in order to 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.

        
// Setting column "A" width
worksheet.Range["A1"].ColumnWidth = 75;

// Use IRange.Characters() to configure font across several runs

// Fetch the IRichText object associated with the cell range
IRichText richText = worksheet.Range["A1"].RichText;

// Add string "GrapeCity " to IRichText object and apply formatting
ITextRun run1 = richText.Add("GrapeCity ");
run1.Font.Color = Color.Red;
run1.Font.Bold = true;
run1.Font.Size = 20;

// Append string "Documents" to IRichText object and apply formatting
ITextRun run2 = richText.Add("Documents");
run2.Font.ThemeFont = ThemeFont.Major;
run2.Font.ThemeColor = ThemeColor.Accent1;
run2.Font.Size = 30;
run2.Font.Underline = 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.Font.Name = "Arial Black";
run3.Font.Color = Color.LightGreen;
run3.Font.Color = Color.LightGreen;
run3.Font.Size = 36;
run3.Font.Italic = true;

// Create composite run
// Extract character range composed of "City" word from run1 and " for" word and apply formatting
ITextRun compositeRun = worksheet.Range["A1"].Characters(5, 18);
compositeRun.Font.Bold = true;
compositeRun.Font.Italic = true;
compositeRun.Font.ThemeColor = 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.

        
// Setting column "A" width
worksheet.Range["A1"].ColumnWidth = 70;

// Using ITextRun.InsertAfter() and InsertBefore() to add rich text

// Fetch the IRichText object associated with the cell range
IRichText richText = worksheet.Range["A1"].RichText;

// 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.Font.ThemeFont = ThemeFont.Major;
run2.Font.ThemeColor = ThemeColor.Accent1;
run2.Font.Size = 30;
run2.Font.Underline = UnderlineType.Single;

// Use InsertBefore() to add string "GrapeCity " to run2 and apply formatting
ITextRun run3 = run2.InsertBefore("GrapeCity ");
run3.Font.Color = Color.Red;
run3.Font.Bold = true;
run3.Font.Size = 20;

// Use InsertAfter() to add string "Excel" to run1 and apply formatting
ITextRun run4 = run1.InsertAfter("Excel");
run4.Font.Name = "Arial Black";
run4.Font.Color = Color.LightGreen;
run4.Font.Size = 36;
run4.Font.Italic = true;