I strongly encourage everyone to watch these suggested videos before reading this blog.
How to add Rich Text (Single or Multiple) in Excel Comment using EPPlus?
First, we need to attach one more namespace "OfficeOpenXml.Style".
We have seen in
Part-8 of this video series the AddComment() method of ExcelRange class accepts two things as a parameter. 1st parameter is string comment & the second is the string author. Here, the first string parameter is a rich text. Whenever we assign text is Excel comment, rich text is involved automatically. This rich text takes a first position of comment text that means zero index position.
Actually, ExcelComment class object accepts one or more rich text. We can add multiple rich texts by using RichText property of ExcelComment class. This RichText property is the type of ExcelRichTextCollection class. Indirectly, this collection class is involve by the add() method of this collection class. We can assign each rich text one by one, starting from zero based index positions.
Please see this below code.
- using(ExcelRange Rng = wsSheet1.Cells["B5"]) {
- Rng.Value = "Add Multiple Rich Text in Excel Comment by Index Position.";
- Rng.Style.Font.Color.SetColor(Color.Red);
- ExcelComment cmd = Rng.AddComment("Rich Text [Index 0]\n", "Rajdip");
-
- cmd.RichText.Add("Rich Text [Index 1]\n");
- cmd.RichText.Add("Rich Text [Index 2]\n");
- cmd.RichText.Add("Rich Text [Index 3]");
- cmd.Visible = true;
- }
How to add Multi Style Rich Text in Excel Comment using EPPlus?
Please see this below code.
- using(ExcelRange Rng = wsSheet1.Cells["B10"]) {
- Rng.Value = "Add Multi Style Rich Text in Excel Comment.";
- Rng.Style.Font.Color.SetColor(Color.Red);
- ExcelComment cmd = Rng.AddComment("Everyday Be Coding ", "Rajdip");
- cmd.Font.Bold = true;
- cmd.Font.UnderLine = true;
- cmd.Font.Italic = true;
- cmd.Font.Color = Color.Red;
- cmd.Font.FontName = "Arial Rounded MT Bold";
-
- ExcelRichText RichText1 = cmd.RichText.Add("is my ");
- RichText1.Bold = true;
- RichText1.Italic = false;
- RichText1.Color = Color.Green;
- RichText1.FontName = "Arial Narrow";
-
- ExcelRichText RichText2 = cmd.RichText.Add("YouTube ");
- RichText2.Bold = true;
- RichText2.Italic = false;
- RichText2.Color = Color.Blue;
- RichText2.FontName = "Arial";
-
- ExcelRichText RichText3 = cmd.RichText.Add("Channel.");
- RichText3.Bold = true;
- RichText3.Italic = false;
- RichText3.Color = Color.Brown;
- RichText3.FontName = "Arial Black";
- cmd.Visible = true;
- }
We can add style of excel comment rich text in two way, first we need to use Font (This property is the type of ExcelRichText class) property of ExcelComment class (we have seen in
Part-10(B) of this video series) or we can directly use ExcelRichText class. This ExcelRichText following properties. Please see this below picture.
How Remove Rich Text in Excel Comment using EPPlus?
We can also remove ExcelRichText object using RemoveAt(int Index) method (by index position) of ExcelRichTextCollection class.
In this code, RichText is the property of ExcelComment class & RichText property is the type of ExcelRichTextCollection class.
Please see this below code.
- using(ExcelRange Rng = wsSheet1.Cells["B15"]) {
- Rng.Value = "Remove Rich Text in Excel Comment by Index Position.";
- Rng.Style.Font.Color.SetColor(Color.Red);
- ExcelComment cmd = Rng.AddComment("Rich Text [Index 0]\n", "Rajdip");
- cmd.RichText.Add("Rich Text [Index 1]\n");
- cmd.RichText.Add("Rich Text [Index 2]\n");
- cmd.RichText.Add("Rich Text [Index 3]");
-
- cmd.RichText.RemoveAt(2);
-
- cmd.Visible = true;
- }
Output in Excel Sheet