How To Set Text Alignment And Font Style In Excel Comment Using EPPlus Library - Part Ten

How do we set text alignment & font style in Excel comments using EPPlus library? What are different types of formatting for Excel comments?
  • Resize/Auto fit comment. (We have seen in Part 9(A))
  • Add text or a background color in the comment box. (We have seen in Part 9(A))
  • Set text alignments in Excel comments.
  • Set font style in Excel comments.
  • Add rich text in Excel comments. (We will be discuss in Part-11(C))
  • Add multi color rich text in Excel cell & comments. (We will discuss in Part-11(C))
  • Set line or border style in Excel comments. (We will be discuss in Part-12(C))
Apply text alignment in Excel comments using EPPlus 
  • First, we need to attach OfficeOpenXml.Drawing.Vml, this namespace is required for text alignment of excel comment.
  • In the Part-4 video, we have seen how to apply text alignment in Excel cell text but in this video I will show you how to apply text alignment in Excel comments. There are two type of alignments 1) VerticalAlignment & 2) HorizontalAlignment.
  • These alignments are the property of Excel comment class and these are assigned by eTextAlignVerticalVml and eTextAlignHorizontalVml enum.


    1. using(ExcelRange Rng = wsSheet1.Cells["B5"]) {  
    2.     Rng.Value = "Everyday Be Coding";  
    3.     ExcelComment cmd = Rng.AddComment(LongText, "Rajdip");  
    4.     cmd.AutoFit = true;  
    5.     cmd.Visible = true;  
    6.     //set alignment in excel comment  
    7.     cmd.VerticalAlignment = eTextAlignVerticalVml.Center;  
    8.     cmd.HorizontalAlignment = eTextAlignHorizontalVml.Center;  
    9. }  
  • In this above code VerticalAlignment & HorizontalAlignment properties are assigned by eTextAlignVerticalVml & eTextAlignHorizontalVml enum.
Apply Font Style in Excel Comment Box using EPPlus?



Please see this below Code
  1. using(ExcelRange Rng = wsSheet1.Cells["B12"]) {  
  2.     Rng.Value = "https://www.facebook.com/EverydayBeCoding/";  
  3.     ExcelComment cmd = Rng.AddComment("This a facebook page URL of my YouTube Channel.""Rajdip");  
  4.     cmd.Font.FontName = "Arial Black";  
  5.     cmd.Font.Color = Color.Red;  
  6.     cmd.Font.Size = 20;  
  7.     cmd.Font.Bold = true;  
  8.     cmd.Font.Italic = true;  
  9.     cmd.Font.UnderLine = true;  
  10.     //cmd.Font.Strike = true;  
  11.     cmd.Visible = true;  
  12. }  
You can set Font style by using Font Property and this Font property has FontName, Size, Bold, Italic, UnderLine, Strike, VerticalAlignproperties. These are properties of ExcelRichText class. We discussed theExcelRichText class in very great detail in Part 11(C) of this video series.
Output in Excel Sheet

Full Source code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using OfficeOpenXml;  
  7. using System.IO;  
  8. using System.Drawing;  
  9. using OfficeOpenXml.Drawing;  
  10. using OfficeOpenXml.Style;  
  11. using OfficeOpenXml.Drawing.Vml;  
  12. namespace EpplusDemo {  
  13.     class Program {  
  14.         static void Main(string[] args) {  
  15.             //Code download from: https://everyday-be-coding.blogspot.in/p/epplus-library-part-10.html  
  16.             //Author: Rajdip Sarkar.  
  17.             //Date : 6th June 2017.  
  18.             //My YouTube Channel Link : https://www.youtube.com/channel/UCpGuQx5rDbWnc7i_qKDTRSQ  
  19.             ExcelPackage ExcelPkg = new ExcelPackage();  
  20.             ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");  
  21.             using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {  
  22.                 Rng.Value = "Everyday Be Coding - Excel COMMENTS Formatting using EPPlus .Net Library";  
  23.                 Rng.Style.Font.Size = 16;  
  24.                 Rng.Style.Font.Bold = true;  
  25.                 Rng.Style.Font.Italic = true;  
  26.             }  
  27.             string LongText = "We are offering very easy level beginner tutorials\n on Microsoft .NET base platform, basically for fresher\n as well as experience candidates & also we are focusing\n on very uncommon & specific topics those are extremely\n useful on real life software development.";  
  28.             //How to Set Text Alignment in Excel Comment Box  
  29.             using(ExcelRange Rng = wsSheet1.Cells["B5"]) {  
  30.                 Rng.Value = "Everyday Be Coding";  
  31.                 ExcelComment cmd = Rng.AddComment(LongText, "Rajdip");  
  32.                 cmd.AutoFit = true;  
  33.                 cmd.Visible = true;  
  34.                 cmd.VerticalAlignment = eTextAlignVerticalVml.Center;  
  35.                 cmd.HorizontalAlignment = eTextAlignHorizontalVml.Right;  
  36.             }  
  37.             //How to Set Font Style in Excel Comment Box  
  38.             using(ExcelRange Rng = wsSheet1.Cells["B12"]) {  
  39.                 Rng.Value = "https://www.facebook.com/EverydayBeCoding/";  
  40.                 ExcelComment cmd = Rng.AddComment("This a facebook page URL of my YouTube Channel.""Rajdip");  
  41.                 cmd.Font.FontName = "Arial Black";  
  42.                 cmd.Font.Size = 20;  
  43.                 cmd.Font.Bold = true;  
  44.                 cmd.Font.Italic = true;  
  45.                 cmd.Font.UnderLine = true;  
  46.                 //cmd.Font.Strike = true;  
  47.                 cmd.Visible = true;  
  48.             }  
  49.             wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();  
  50.             ExcelPkg.SaveAs(new FileInfo(@ "D:\Comments.xlsx"));  
  51.         }  
  52.     }  
  53. }  
Now build & execute this code. File is (FormatComments.xlsx) stored on D: drive of computer.
Thank you for reading this blog.

YouTube :https://goo.gl/rt4tHH
Facebook :https://goo.gl/m2skDb
Twitter :https://goo.gl/nUwGnf
Ebook Download
View all
Learn
View all