menu

ASP.NET Core

  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Interface IRichTextString

    Show / Hide Table of Contents

    Interface IRichTextString

    Represents a Rich Text String that can be used to apply several styles inside a single cell.

    Inherited Members
    IParentApplication.Application
    IParentApplication.Parent
    IOptimizedUpdate.BeginUpdate()
    IOptimizedUpdate.EndUpdate()
    Namespace: Syncfusion.XlsIO
    Assembly: Syncfusion.XlsIO.Base.dll
    Syntax
    public interface IRichTextString : IParentApplication, IOptimizedUpdate

    Properties

    IsFormatted

    Indicates whether rich text string has formatting runs. Read-only.

    Declaration
    bool IsFormatted { get; }
    Property Value
    Type Description
    System.Boolean
    Examples

    The following code illustrates how to access IsFormatted property.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Check IsFormatted
                Console.Write(richText.IsFormatted);
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
                Console.ReadKey();
            }
    //Output will be
    //True

    RtfText

    Returns text in rtf format. Read-only.

    Declaration
    string RtfText { get; set; }
    Property Value
    Type Description
    System.String
    Examples

    The following code illustrates how to access the RtfText of the RichText.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Get rtf text string
                Console.Write(richText.RtfText);
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
                Console.ReadKey();
            }
    //Output will be
    //{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset1 Calibri;}}{\colortbl;\red255\green0\blue0;}{\f0\fs22\cf1\u83*\u97*\u109*\u112*\u108*\u101*}}

    Text

    Gets / sets text of the string.

    Declaration
    string Text { get; set; }
    Property Value
    Type Description
    System.String
    Examples

    The following code illustrates how to set Text for the RichText.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample text";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Set color
                font.Color = ExcelKnownColors.Blue;
    
                //Set rich text font
                richText.SetFont(6, 10, font);
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
            }

    Methods

    Append(String, IFont)

    Appends rich text string with specified text and font.

    Declaration
    void Append(string text, IFont font)
    Parameters
    Type Name Description
    System.String text

    Text to append.

    IFont font

    Font to use.

    Examples

    The following code illustrates how to append RichText to an existing RichText.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Set color
                font.Color = ExcelKnownColors.Blue;
    
                //Append text
                richText.Append("Text", font);
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
            }

    Clear()

    Clears text and formatting.

    Declaration
    void Clear()
    Examples

    The following code illustrates how to clear RichText's text and formatting applied.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Add style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set style
                worksheet["C2"].CellStyle = style;
    
                //Set text
                worksheet["C2"].Text = "Sample text";
    
                //Set RichText
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Get font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set format to RichText
                richText.SetFont(0, 5, font);
    
                //Clear RichText
                richText.Clear();
    
                //Save and Dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
            }

    ClearFormatting()

    Clears string formatting.

    Declaration
    void ClearFormatting()
    Examples

    The following code illustrates how to clear the formatting applied to the RichText.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample text";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Set color
                font.Color = ExcelKnownColors.Blue;
    
                //Set rich text font
                richText.SetFont(6, 10, font);
    
                //Clear format
                richText.ClearFormatting();
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
            }

    GetFont(Int32)

    Returns font which is applied to character at the specified position.

    Declaration
    IFont GetFont(int iPosition)
    Parameters
    Type Name Description
    System.Int32 iPosition

    Character index.

    Returns
    Type Description
    IFont

    Font which is applied to character at the specified position.

    Remarks

    Font style can be changed by cloning the font obtained using GetFont method and to be set using SetFont(Int32, Int32, IFont) method.

    Examples

    The following code illustrates how to access GetFont method.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Add style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set style
                worksheet["C2"].CellStyle = style;
    
                //Set text
                worksheet["C2"].Text = "Sample text";
    
                //Set RichText
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Get font
                IFont font = richText.GetFont(1);
    
                //Get font name
                Console.Write(font.FontName);
    
                //Save and Dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
                Console.ReadKey();
            }
    //Output will be
    //Calibri
    Exceptions
    Type Condition
    System.ArgumentOutOfRangeException

    Since the IFont object returned by GetFont method is read-only Argument out of range exception will occur if it is modified

    SetFont(Int32, Int32, IFont)

    Sets font for range of characters.

    Declaration
    void SetFont(int iStartPos, int iEndPos, IFont font)
    Parameters
    Type Name Description
    System.Int32 iStartPos

    First character of the range.

    System.Int32 iEndPos

    Last character of the range.

    IFont font

    Font to set.

    Remarks

    To know more about Rich-Text refer Rich-Text Formatting

    Examples

    The following code illustrates how a font can be set to a substring of the RichText.

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Create worksheet
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
    
                //Create style
                IStyle style = workbook.Styles.Add("CustomStyle");
    
                //Set rich text
                IRichTextString richText = worksheet["C2"].RichText;
    
                //Set text
                richText.Text = "Sample text";
    
                //Set font
                IFont font = style.Font;
    
                //Set color
                font.Color = ExcelKnownColors.Red;
    
                //Set rich text font
                richText.SetFont(0, 5, font);
    
                //Set color
                font.Color = ExcelKnownColors.Blue;
    
                //Set rich text font
                richText.SetFont(6, 10, font);
    
                //Save and dispose
                workbook.SaveAs("CellFormats.xlsx");
                workbook.Close();
            }
    Back to top Generated by DocFX
    Copyright © 2001 - 2025 Syncfusion Inc. All Rights Reserved