Spreadsheet Document API – Rich Text (v18.2)

The DevExpress Spreadsheet Document API allows you to apply different fonts and colors to desired regions of spreadsheet cell text. 

Rich Text in API

Rich text within a cell is comprised of one or more text regions (or text runs), each with its own set of font characteristics. 

Our Spreadsheet Document API’s Cell interface has new methods and properties designed to manipulate rich text within a cell: 

  • The HasRichText property helps you determine whether a cell already contains rich text.
  • The GetRichText method retrieves rich text from a cell. This method returns the RichTextString object. It has been designed to view and edit rich text settings. This is the root object of our Rich Text API.
  • The SetRichText method assigns rich formatted text to a cell. The resulting text string is also accessible as plain text using the cell’s Value property.

Please note that you can only use our Rich Text API for cells with text values. You cannot apply rich formatting to numbers or dates. 

Create Rich Text

You can apply rich formatting to cell text by: 

  • Using the RichTextString.Characters method to format specific characters within an existing cell:
    // Add text to the cell B2.
    worksheet["B2"].Value = "Rich text formatting";
    
    // Obtain a RichTextString object containing the cell text.
    RichTextString richText = worksheet["B2"].GetRichText();
    // Format the first word as bold. 
    richText.Characters(0, 4).Font.Bold = true;
    
    // Assign rich formatted text to the cell.
    worksheet["B2"].SetRichText(richText);
    
  • Using the RichTextString.AddTextRun method to compose cell text from individual text runs:
    // Create a RichTextString instance.
    RichTextString richText = new RichTextString();
    
    // Add three text runs with the specified font settings.
    richText.AddTextRun("Rich ", new RichTextRunFont("Arial", 14, 
      System.Drawing.Color.FromArgb(0xc5, 0x9f, 0xc9)));
    richText.AddTextRun("text ", new RichTextRunFont("Tahoma", 14, 
      System.Drawing.Color.FromArgb(0x2c, 0x60, 0x8e)));
    richText.AddTextRun("formatting", new RichTextRunFont("Castellar", 14, 
      System.Drawing.Color.FromArgb(0x2f, 0x24, 0x4f)));
    
    // Assign rich formatted text to the cell B2.
    worksheet["B2"].SetRichText(richText);
    

Let us know your thoughts

Your feedback is always welcome. Please take a moment to answer the following question so we can better understand your needs and plan for future iterations of our Spreadsheet product line. 

no comments
No Comments

Please login or register to post comments.