C#-Write Rich Text in Excel

In the last two post, we have learned how to write and read comments with C#. And we can find that one of the two comments is rich text comment.

What is rich text? Literally speaking, it is several words with different formats, for example, to set the first word as bold and the second one with different color.

In this post, I want to introduce method to write rich text in Excel.

Firstly, create a new project and name it WriteRichText.

Secondly, add references. Of course, I use Spire.XLS, so add DLL file as reference.

Thirdly, create a new workbook and initialize worksheet. After that, create rich text font by using Createfont() and set format. Finally, write rich text to Excel. Use sheet.Range[] to confirm where to write text. And richText.SetFont to set font for specified ranges of characters.

Use the following code:

using System;
using Spire.Xls;

namespace WriteRichText
    class Program
        static void Main(string[] args)
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            //Create Rich Text Font
            ExcelFont fontBold = workbook.CreateFont();
            fontBold.FontName = "Comic Sans MS";
            fontBold.Size = 12;
            fontBold.IsBold = true;

            ExcelFont fontUnderline = workbook.CreateFont();
            fontUnderline.FontName = "Comic Sans MS";
            fontUnderline.Size = 12;
            fontUnderline.Underline = FontUnderlineType.Single;

            ExcelFont fontColor = workbook.CreateFont();
            fontColor.FontName = "Comic Sans MS";
            fontColor.Size = 12;
            fontColor.KnownColor = ExcelColors.Lime;

            //Write Rich Text
            RichText richText = sheet.Range["A1"].RichText;
            richText.Text = "Bold underlined colored text";

            richText.SetFont(0, 3, fontBold);
            richText.SetFont(5, 14, fontUnderline);
            richText.SetFont(16, 27, fontColor);
            //Save File

            //Launch File



Click Here to learn more about Spire.XLS

Click Here to download Spire.XLS


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s