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:
static void Main(string args)
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;
//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);
Click Here to learn more about Spire.XLS
Click Here to download Spire.XLS