Style Cells, Borders, and Fonts in Excel with C#

IronXL allows C# developers to style any Excel Cell and Range in C#. IronXL covers most of the styling available in Microsoft Excel, such as fonts, text alignment, borders, colors, backgrounds, and patterns. The code example below shows a wide range of styles configurable with IronXL.

Individual Cell and Range borders (bottom, top, left, right, and diagonal) can be set to 14 different border styles such as dotted, dashed, and double, to name a few, using the enum BorderType. You can enable or disable the ShrinkToFit and WrapText properties to have more control over how the spreadsheet organizes cell sizes.

// Ensure the IronXL package is installed via NuGet Package Manager

using IronXL;
using IronXL.Styles;

class ExcelStyler
{
    static void Main(string[] args)
    {
        // Load an existing Excel file
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet in the workbook
        WorkSheet worksheet = workbook.WorkSheets.First();

        // Create a new style or modify an existing style
        Style style = worksheet["A1"].Style;

        // Set font properties
        style.Font.Size = 14; // Set font size
        style.Font.Bold = true; // Set bold font weight
        style.Font.Italic = true; // Set italic font style
        style.Font.Underline = IronXL.Styles.Underline.Single; // Set single underline

        // Set border properties
        style.Border.Bottom.Type = BorderType.Thick; // Set a thick bottom border
        style.Border.Bottom.Color = "#FF0000"; // Set bottom border color to red

        style.Border.Top.Type = BorderType.Dashed; // Set a dashed top border
        style.Border.Right.Type = BorderType.Double; // Set a double right border

        // Set some additional cell properties
        style.ShrinkToFit = true; // Enable Shrink to Fit
        style.WrapText = false; // Disable text wrapping

        // Apply the style to a specific cell
        worksheet["A1"].Style = style;

        // Save changes to the workbook
        workbook.SaveAs("formatted_example.xlsx");
    }
}
// Ensure the IronXL package is installed via NuGet Package Manager

using IronXL;
using IronXL.Styles;

class ExcelStyler
{
    static void Main(string[] args)
    {
        // Load an existing Excel file
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet in the workbook
        WorkSheet worksheet = workbook.WorkSheets.First();

        // Create a new style or modify an existing style
        Style style = worksheet["A1"].Style;

        // Set font properties
        style.Font.Size = 14; // Set font size
        style.Font.Bold = true; // Set bold font weight
        style.Font.Italic = true; // Set italic font style
        style.Font.Underline = IronXL.Styles.Underline.Single; // Set single underline

        // Set border properties
        style.Border.Bottom.Type = BorderType.Thick; // Set a thick bottom border
        style.Border.Bottom.Color = "#FF0000"; // Set bottom border color to red

        style.Border.Top.Type = BorderType.Dashed; // Set a dashed top border
        style.Border.Right.Type = BorderType.Double; // Set a double right border

        // Set some additional cell properties
        style.ShrinkToFit = true; // Enable Shrink to Fit
        style.WrapText = false; // Disable text wrapping

        // Apply the style to a specific cell
        worksheet["A1"].Style = style;

        // Save changes to the workbook
        workbook.SaveAs("formatted_example.xlsx");
    }
}
' Ensure the IronXL package is installed via NuGet Package Manager

Imports IronXL
Imports IronXL.Styles

Friend Class ExcelStyler
	Shared Sub Main(ByVal args() As String)
		' Load an existing Excel file
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Select the first worksheet in the workbook
		Dim worksheet As WorkSheet = workbook.WorkSheets.First()

		' Create a new style or modify an existing style
		Dim style As Style = worksheet("A1").Style

		' Set font properties
		style.Font.Size = 14 ' Set font size
		style.Font.Bold = True ' Set bold font weight
		style.Font.Italic = True ' Set italic font style
		style.Font.Underline = IronXL.Styles.Underline.Single ' Set single underline

		' Set border properties
		style.Border.Bottom.Type = BorderType.Thick ' Set a thick bottom border
		style.Border.Bottom.Color = "#FF0000" ' Set bottom border color to red

		style.Border.Top.Type = BorderType.Dashed ' Set a dashed top border
		style.Border.Right.Type = BorderType.Double ' Set a double right border

		' Set some additional cell properties
		style.ShrinkToFit = True ' Enable Shrink to Fit
		style.WrapText = False ' Disable text wrapping

		' Apply the style to a specific cell
		worksheet("A1").Style = style

		' Save changes to the workbook
		workbook.SaveAs("formatted_example.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • Comments: This C# code snippet shows how to style an Excel cell using the IronXL library. Key functionalities include setting font size, weight, underline styles, and customizing border styles for cells. It also demonstrates saving the modified Excel file using IronXL.