Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Explanation:
References: The code requires IronXL and IronSoftware.Drawing. Ensure these packages are installed in your project.
Workbook and Worksheet Creation: We create a new workbook and a worksheet named "SampleSheet".
Cell Customization: We customize cell B2 by applying thick borders to the left and right with specified colors.
Alignment and Text Setting: (Optional) We set the horizontal and vertical alignment of cell B2 for better display.
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