How To Apply Cell Border Style In An Excel Sheet Using EPPlus .NET Application (C#) - Part Three

Excel Cell Border alignment
  • EPPlus supports Top, Left, Right, Bottom, Diagonal, DiagonalDown,DiagonalUp alignments. These alignment properties are assigned by different types of ExcelBorderStyle class properties.

  • For Example: Thin, Medium, Thick, DashDot, DashDotDot, Dashed, Dotted,Double, Hair, MediumDashDot, MediumDashDotDot, MediumDashed, none.
Example
  • ExcelRange Rng = new ExcelRange();
  • Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin; Rng.Style.Border.Left.Style = ExcelBorderStyle.Medium; Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin; Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thick;
*By default, EPPlus supports no border, if you are not specifying any border style. Now, the next question in our mind is how to apply cell border color?

Example
  • Rng.Style.Border.Top.Color.SetColor(Color.Red);
  • Here SetColor method can support structure Color property as a parameter.
  • You can also specify the HTML Color code.
For Example
  • Color DeepBlueHexCode = ColorTranslator.FromHtml("#254061");
  • Rng.Style.Border.Top.Color.SetColor(DeepBlueHexCode);
  • In this example FromHtml() directly accept HTML RGB (Red Green Blue) color Code.
Output on an Excel sheet is given below.



Source code
  1. using OfficeOpenXml;  
  2. using System.IO;  
  3. using System;  
  4. //add two new namespace  
  5. using OfficeOpenXml.Style;  
  6. using System.Drawing;  
  7. class Program {  
  8.     static void Main(string[] args) {  
  9.         ExcelPackage ExcelPkg = new ExcelPackage();  
  10.         ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");  
  11.         using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {  
  12.             Rng.Value = "Welcome to Everyday be coding - tutorials for beginners";  
  13.             Rng.Style.Font.Size = 16;  
  14.             Rng.Style.Font.Bold = true;  
  15.             Rng.Style.Font.Italic = true;  
  16.         }  
  17.         Color DeepBlueHexCode = ColorTranslator.FromHtml("#254061");  
  18.         //First Border Box  
  19.         using(ExcelRange Rng = wsSheet1.Cells[5, 2, 8, 4]) {  
  20.             Rng.Value = "Thin";  
  21.             Rng.Merge = true;  
  22.             Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  23.             Rng.Style.Border.Top.Color.SetColor(Color.Red);  
  24.             Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  25.             Rng.Style.Border.Left.Color.SetColor(Color.Green);  
  26.             Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  27.             Rng.Style.Border.Right.Color.SetColor(Color.Green);  
  28.             Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  29.             Rng.Style.Border.Bottom.Color.SetColor(DeepBlueHexCode);  
  30.         }  
  31.         wsSheet1.Protection.IsProtected = false;  
  32.         wsSheet1.Protection.AllowSelectLockedCells = false;  
  33.         ExcelPkg.SaveAs(new FileInfo(@ "D:\New.xlsx"));  
  34.     }  
  35. }  
Now, build & execute the code. File is (New.xlsx), which is stored on D: drive of the computer.

Thank you for reading this blog.