How to Set Cell Borders and Alignment in Excel

In this tutorial, we explore how to set cell borders and alignment in Excel using C# with IronXL and the Iron Software Drawing Library. After ensuring that the necessary packages are installed, we dive into the code. The tutorial demonstrates creating a new workbook and worksheet, and specifically focuses on customizing cell B2. We apply thick borders to the left and right sides of the cell, using aquamarine and coral colors, respectively. By running the project, we confirm that the cell's appearance in Excel matches our expectations. IronXL offers a variety of border styles such as thin, medium, thick, dotted, and more. Additionally, it supports multiple alignment options like General, Right, Top, and Left. These features allow for extensive customization of Excel worksheets. The tutorial concludes by encouraging viewers to try out the different border and alignment options available and offers a 30-day trial for those interested in experimenting further with the software.

using IronXL;
using IronSoftware.Drawing;
using System.Drawing;

class ExcelCustomization
{
    static void Main()
    {
        // Create a new workbook and worksheet
        var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
        var sheet = workbook.CreateWorkSheet("SampleSheet");

        // Access cell B2
        var cell = sheet["B2"];

        // Apply thick border to the left and right of the cell
        cell.Style.BorderLeft.Type = IronXL.Styles.BorderType.Thick;
        cell.Style.BorderLeft.Color = Color.Aquamarine;

        cell.Style.BorderRight.Type = IronXL.Styles.BorderType.Thick;
        cell.Style.BorderRight.Color = Color.Coral;

        // Optionally, you could set alignment here
        cell.Style.Alignment.Horizontal = IronXL.Styles.HorizontalAlignment.Left;
        cell.Style.Alignment.Vertical = IronXL.Styles.VerticalAlignment.Center;

        // Set some text for verification
        cell.Value = "Customized B2";

        // Save the workbook to the specified path
        workbook.SaveAs("CustomizedExcel.xlsx");
    }
}
using IronXL;
using IronSoftware.Drawing;
using System.Drawing;

class ExcelCustomization
{
    static void Main()
    {
        // Create a new workbook and worksheet
        var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
        var sheet = workbook.CreateWorkSheet("SampleSheet");

        // Access cell B2
        var cell = sheet["B2"];

        // Apply thick border to the left and right of the cell
        cell.Style.BorderLeft.Type = IronXL.Styles.BorderType.Thick;
        cell.Style.BorderLeft.Color = Color.Aquamarine;

        cell.Style.BorderRight.Type = IronXL.Styles.BorderType.Thick;
        cell.Style.BorderRight.Color = Color.Coral;

        // Optionally, you could set alignment here
        cell.Style.Alignment.Horizontal = IronXL.Styles.HorizontalAlignment.Left;
        cell.Style.Alignment.Vertical = IronXL.Styles.VerticalAlignment.Center;

        // Set some text for verification
        cell.Value = "Customized B2";

        // Save the workbook to the specified path
        workbook.SaveAs("CustomizedExcel.xlsx");
    }
}
Imports IronXL
Imports IronSoftware.Drawing
Imports System.Drawing

Friend Class ExcelCustomization
	Shared Sub Main()
		' Create a new workbook and worksheet
		Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
		Dim sheet = workbook.CreateWorkSheet("SampleSheet")

		' Access cell B2
		Dim cell = sheet("B2")

		' Apply thick border to the left and right of the cell
		cell.Style.BorderLeft.Type = IronXL.Styles.BorderType.Thick
		cell.Style.BorderLeft.Color = Color.Aquamarine

		cell.Style.BorderRight.Type = IronXL.Styles.BorderType.Thick
		cell.Style.BorderRight.Color = Color.Coral

		' Optionally, you could set alignment here
		cell.Style.Alignment.Horizontal = IronXL.Styles.HorizontalAlignment.Left
		cell.Style.Alignment.Vertical = IronXL.Styles.VerticalAlignment.Center

		' Set some text for verification
		cell.Value = "Customized B2"

		' Save the workbook to the specified path
		workbook.SaveAs("CustomizedExcel.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  1. References: The code requires IronXL and IronSoftware.Drawing. Ensure these packages are installed in your project.

  2. Workbook and Worksheet Creation: We create a new workbook and a worksheet named "SampleSheet".

  3. Cell Customization: We customize cell B2 by applying thick borders to the left and right with specified colors.

  4. Alignment and Text Setting: (Optional) We set the horizontal and vertical alignment of cell B2 for better display.

  5. Saving the Workbook: The workbook is saved with the name "CustomizedExcel.xlsx".

By running this code, you will be able to see the customized appearance of cell B2 in your generated Excel file.

Further Reading: How to Set Cell Border and Alignment

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Use Excel Interop in C# Alternatively
NEXT >
How to Read Excel File in Blazor NET