Back to Devexpress

XlRichTextString Class

corelibraries-devexpress-dot-export-dot-xl-9330a70c.md

latest5.2 KB
Original Source

XlRichTextString Class

Represents a rich formatted text in a cell.

Namespace : DevExpress.Export.Xl

Assembly : DevExpress.Printing.v25.2.Core.dll

NuGet Package : DevExpress.Printing.Core

Declaration

csharp
public class XlRichTextString :
    IXlString
vb
Public Class XlRichTextString
    Implements IXlString

Remarks

A cell in a worksheet may contain a rich formatted text specified by the XlRichTextString object. Such rich text represents a cell text divided into one or more text regions (so-called text runs), each of which has its own set of font characteristics. Each text run is defined by the XlRichTextRun object and is stored in the collection of runs accessible from the XlRichTextString.Runs property.

To specify the rich formatting for the cell text, create an instance of the XlRichTextString class, add the required number of XlRichTextRun objects to the XlRichTextString.Runs collection, and then pass the specified XlRichTextString instance to the IXlCell.SetRichText method as a parameter. To obtain the entire text contained in a cell, use the XlRichTextString.Text property.

For more information on how to apply different fonts to specific regions of the cell text, refer to the How to: Apply Rich Formatting to the Cell Text example.

Example

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

csharp
// Create a new worksheet.
using (IXlSheet sheet = document.CreateSheet())
{
    // Create the first column and set its width.
    using (IXlColumn column = sheet.CreateColumn())
    {
        column.WidthInPixels = 180;
    }
    // Create the first row.
    using (IXlRow row = sheet.CreateRow())
    {
        // Create the cell A1.
        using (IXlCell cell = row.CreateCell())
        {
            // Create an XlRichTextString instance.
            XlRichTextString richText = new XlRichTextString();
            // Add three text runs to the collection. 
            richText.Runs.Add(new XlRichTextRun("Formatted ", XlFont.CustomFont("Arial", 14.0, XlColor.FromArgb(0x53, 0xbb, 0xf4))));
            richText.Runs.Add(new XlRichTextRun("cell ", XlFont.CustomFont("Century Gothic", 14.0, XlColor.FromArgb(0xf1, 0x77, 0x00))));
            richText.Runs.Add(new XlRichTextRun("text", XlFont.CustomFont("Consolas", 14.0, XlColor.FromArgb(0xe3, 0x2c, 0x2e))));
            // Add the rich formatted text to the cell. 
            cell.SetRichText(richText);
        }
    }
}
vb
' Create a new worksheet.
Using sheet As IXlSheet = document.CreateSheet()
    ' Create the first column and set its width.
    Using column As IXlColumn = sheet.CreateColumn()
        column.WidthInPixels = 180
    End Using
    ' Create the first row.
    Using row As IXlRow = sheet.CreateRow()
        ' Create the cell A1.
        Using cell As IXlCell = row.CreateCell()
            ' Create an XlRichTextString instance.
            Dim richText As New XlRichTextString()
            ' Add three text runs to the collection. 
            richText.Runs.Add(New XlRichTextRun("Formatted ", XlFont.CustomFont("Arial", 14.0, XlColor.FromArgb(&H53, &Hbb, &Hf4))))
            richText.Runs.Add(New XlRichTextRun("cell ", XlFont.CustomFont("Century Gothic", 14.0, XlColor.FromArgb(&Hf1, &H77, &H0))))
            richText.Runs.Add(New XlRichTextRun("text", XlFont.CustomFont("Consolas", 14.0, XlColor.FromArgb(&He3, &H2c, &H2e))))
            ' Add the rich formatted text to the cell. 
            cell.SetRichText(richText)
        End Using
    End Using
End Using

Inheritance

Object XlRichTextString

See Also

XlRichTextString Members

XlRichTextRun

Use the Excel Export API to Apply Rich Formatting to the Cell Text

DevExpress.Export.Xl Namespace