Spread Windows Forms 15.0
Spread Windows Forms 15.0 Product Documentation / Developer's Guide / Cells / Setting Rich Text in a Cell
In This Topic
    Setting Rich Text in a Cell
    In This Topic

    Spread for Winforms provides the ability to add rich text in a cell. You can add different rich text formatting options such as fonts, text styles, colors, superscripts, and subscripts.

    The RichText Class can be used to set a rich text instance consisting of the string that will have rich text capabilities.

    The following code shows how to add rich text in a cell.

    C#
    Copy Code
    // Initiating a richtext object
    GrapeCity.Spreadsheet.RichText richText = new GrapeCity.Spreadsheet.RichText("This is a rich text");
                
    // Defining font styles
    GrapeCity.Spreadsheet.Font font = GrapeCity.Spreadsheet.Font.Empty;
    GrapeCity.Spreadsheet.Font font2 = GrapeCity.Spreadsheet.Font.Empty;
    GrapeCity.Spreadsheet.Font font3 = GrapeCity.Spreadsheet.Font.Empty;
    GrapeCity.Spreadsheet.Font font4 = GrapeCity.Spreadsheet.Font.Empty;
    GrapeCity.Spreadsheet.Font fontAll = GrapeCity.Spreadsheet.Font.Empty;
    
    // Setting font styles
    font.VerticalAlign = GrapeCity.Spreadsheet.VerticalTextAlignment.Subscript;
    font2.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Violet);
    font3.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Green);
    font4.Italic = true;
    fontAll.Bold = true;
    fontAll.Size = 24;
    
    // Applying styles to richtext instance
    richText.Format(8, 1, font);
    richText.Format(15, 4, font2);
    richText.Format(0, 4, font3);
    richText.Format(10, 4, font4);
    richText.Format(fontAll);
                
    // Setting rich text in a cell
    TestActiveSheet.Cells["A1"].Value = richText;
    
    Visual Basic
    Copy Code
    'Initiating a richtext object
    Dim richText As GrapeCity.Spreadsheet.RichText = New GrapeCity.Spreadsheet.RichText("This is a rich text")
    
    'Defining font styles
    Dim font As GrapeCity.Spreadsheet.Font = GrapeCity.Spreadsheet.Font.Empty
    Dim font2 As GrapeCity.Spreadsheet.Font = GrapeCity.Spreadsheet.Font.Empty
    Dim font3 As GrapeCity.Spreadsheet.Font = GrapeCity.Spreadsheet.Font.Empty
    Dim font4 As GrapeCity.Spreadsheet.Font = GrapeCity.Spreadsheet.Font.Empty
    Dim fontAll As GrapeCity.Spreadsheet.Font = GrapeCity.Spreadsheet.Font.Empty
    
    'Setting font styles
    font.VerticalAlign = GrapeCity.Spreadsheet.VerticalTextAlignment.Subscript
    font2.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Violet)
    font3.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Green)
    font4.Italic = True
    fontAll.Bold = True
    fontAll.Size = 24
    
    'Applying styles to richtext instance
    richText.Format(8, 1, font)
    richText.Format(15, 4, font2)
    richText.Format(0, 4, font3)
    richText.Format(10, 4, font4)
    richText.Format(fontAll)
    
    'Setting rich text in a cell
    TestActiveSheet.Cells("A1").Value = richText
    
    Note: If you edit the rich text in runtime then it behaves as plain text.

    ExcelIO for Rich Text

    Spreadsheets containing rich text formatting can easily be imported from or exported to Excel by setting FpSpread.Features.RichText property to True.

    The following code shows how to import or export an Excel file containing rich text.

    C#
    Copy Code
    // Enable RichText property
    fpSpread1.Features.RichText = true;
    
    // Import file containing rich text
    fpSpread1.ActiveSheet.OpenExcel("richtext-file.xlsx", 0);
    
    // Export rich text set in cells to a file
    fpSpread1.SaveExcel("excelfile.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat);
    
    Visual Basic
    Copy Code
    'Enable RichText property
    FpSpread1.Features.RichText = True
    
    'Import file containing rich text
    FpSpread1.ActiveSheet.OpenExcel("richtext-file.xlsx", 0)
    
    'Export rich text set in cells to a file
    FpSpread1.SaveExcel("excelfile.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat)
    
    Note: PDF export is not supported.