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.
*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 - using OfficeOpenXml;
- using System.IO;
- using System;
-
- using OfficeOpenXml.Style;
- using System.Drawing;
- class Program {
- static void Main(string[] args) {
- ExcelPackage ExcelPkg = new ExcelPackage();
- ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
- using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {
- Rng.Value = "Welcome to Everyday be coding - tutorials for beginners";
- Rng.Style.Font.Size = 16;
- Rng.Style.Font.Bold = true;
- Rng.Style.Font.Italic = true;
- }
- Color DeepBlueHexCode = ColorTranslator.FromHtml("#254061");
-
- using(ExcelRange Rng = wsSheet1.Cells[5, 2, 8, 4]) {
- Rng.Value = "Thin";
- Rng.Merge = true;
- Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;
- Rng.Style.Border.Top.Color.SetColor(Color.Red);
- Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
- Rng.Style.Border.Left.Color.SetColor(Color.Green);
- Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
- Rng.Style.Border.Right.Color.SetColor(Color.Green);
- Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
- Rng.Style.Border.Bottom.Color.SetColor(DeepBlueHexCode);
- }
- wsSheet1.Protection.IsProtected = false;
- wsSheet1.Protection.AllowSelectLockedCells = false;
- ExcelPkg.SaveAs(new FileInfo(@ "D:\New.xlsx"));
- }
- }
Now, build & execute the code. File is (New.xlsx), which is stored on D: drive of the computer.
Thank you for reading this blog.