Skip to main content

EPPlus Library Part-3

EPPLUS Library - Beginners Guide Part-3
How to apply cell border style in excel sheet?




Hindi Video :  Click here
Previous Video :  Click here [English]
Previous Video :  Click here [Hindi]
Source Code : Click to download [675 KB]

Cell border alignment:
  • Epplus is support Top, Left, Right, Bottom, Diagonal, DiagonalDown, DiagonalUp alignments. These alignments properties are assigned by different type of ExcelBorderStyle class properties. 
  • For Example: Thin, Medium, Thick, DashDot, DashDotDot, Dashed, Dotted, Double, Hair, MediumDashDot, MediumDashDotDot, MediumDashed, none. 
Example:
  • ExcelRange Rng = wsSheet1.Cells["B5:D8"]
  • 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 support none border, if you are not specify any border style. 
Now next question in our mind, 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 in Excel Sheet:


Source code:
using OfficeOpenXml;
using System.IO;
using System;
//add two new namespace
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");
            //First Border Box
            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 this code. File is (New.xlsx) store on D: drive of computer.
Thank you for reading this article. 
Please subscribe my YouTube Channel & don't forget to like and share. 

YouTube :https://goo.gl/rt4tHH
Facebook :https://goo.gl/m2skDb
Twitter :https://goo.gl/nUwGnf

Comments

Popular posts from this blog

Cloud Storage Access through API  - Advanced Developer Guide - C#  1. Google Drive API - Enable & Get Client Credentials for Application 2. Google Drive API - Uploading, Viewing, Downloading & Deleting Files 3. Google Drive API - Create folder, Upload file to folder, Show folder Content. 4. Google Drive API - How to Move & Copy Files Between Folders. 5. Google Drive API - How to Share & Set Permission of File/Folders. 6. Google Drive API - View Share Users Permission Details. 7. Google Picker API - Viewing, Uploading, Downloading, Sharing. ASP.NET  MVC Tutorial - Advanced Developer Guide - C#  How to Export Razor View to Excel file (Without using Third-Party Library). Excel Development using EPPlus Library Beginners Guide - C# 1. Create an Excel File using EPPlus .Net Library. 2. Apply Cell text and Background Color in Excel Sheet. 3. Apply Cell Border Style in Excel Sheet. 4. Apply Cell Text Alignment, Row Height, Col...

Google Drive API using JavaScript | Full Project - Overview

Setup Google Developer Console for Google Drive API Applications using J...