How to Set Cell Background Patterns and Color in Excel

In this tutorial, we explore setting cell background patterns and colors in Excel using the IronXL and Iron Software Drawing Library. After installing the required packages via NuGet, we dive into the code, where the namespaces IronXL and IronXL.Styles are included for Excel manipulation. Aliasing IronSoftware.Drawing as DoColor helps in clarity. We create a new workbook and set background fill patterns for cells A1 and A2. Cell A1 features alternating bars with an aquamarine color, while cell A2 displays thick vertical bands in a color defined by the hex code #AF2F. The workbook is saved as 'set_background_pattern.xlsx'. Running the project shows the successful application of these styles. The tutorial concludes by encouraging viewers to try out the software and explore its capabilities further with a trial subscription link provided.

Further Reading: How to Set Cell Background Pattern & Color

using IronXL;
using IronXL.Styles;
using DoColor = IronSoftware.Drawing.Color; // Alias IronSoftware.Drawing.Color for clarity

public class ExcelBackgroundPatternExample
{
    public static void Main()
    {
        // Create a new workbook and access the active sheet
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Set background pattern for cell A1
        var cellA1 = sheet["A1"];
        cellA1.Value = "Patterned Cell A1";
        cellA1.Style.BackgroundPattern = BackgroundPattern.AlternatingBars;
        cellA1.Style.PatternColor = DoColor.Aquamarine;

        // Set background pattern for cell A2
        var cellA2 = sheet["A2"];
        cellA2.Value = "Patterned Cell A2";
        cellA2.Style.BackgroundPattern = BackgroundPattern.ThickVerticalBands;
        cellA2.Style.PatternColor = DoColor.FromHtml("#AF2F4F"); // Corrected color hex code

        // Save the workbook as 'set_background_pattern.xlsx'
        workbook.SaveAs("set_background_pattern.xlsx");
    }
}
using IronXL;
using IronXL.Styles;
using DoColor = IronSoftware.Drawing.Color; // Alias IronSoftware.Drawing.Color for clarity

public class ExcelBackgroundPatternExample
{
    public static void Main()
    {
        // Create a new workbook and access the active sheet
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Set background pattern for cell A1
        var cellA1 = sheet["A1"];
        cellA1.Value = "Patterned Cell A1";
        cellA1.Style.BackgroundPattern = BackgroundPattern.AlternatingBars;
        cellA1.Style.PatternColor = DoColor.Aquamarine;

        // Set background pattern for cell A2
        var cellA2 = sheet["A2"];
        cellA2.Value = "Patterned Cell A2";
        cellA2.Style.BackgroundPattern = BackgroundPattern.ThickVerticalBands;
        cellA2.Style.PatternColor = DoColor.FromHtml("#AF2F4F"); // Corrected color hex code

        // Save the workbook as 'set_background_pattern.xlsx'
        workbook.SaveAs("set_background_pattern.xlsx");
    }
}
Imports IronXL
Imports IronXL.Styles
Imports DoColor = IronSoftware.Drawing.Color ' Alias IronSoftware.Drawing.Color for clarity

Public Class ExcelBackgroundPatternExample
	Public Shared Sub Main()
		' Create a new workbook and access the active sheet
		Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
		Dim sheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")

		' Set background pattern for cell A1
		Dim cellA1 = sheet("A1")
		cellA1.Value = "Patterned Cell A1"
		cellA1.Style.BackgroundPattern = BackgroundPattern.AlternatingBars
		cellA1.Style.PatternColor = DoColor.Aquamarine

		' Set background pattern for cell A2
		Dim cellA2 = sheet("A2")
		cellA2.Value = "Patterned Cell A2"
		cellA2.Style.BackgroundPattern = BackgroundPattern.ThickVerticalBands
		cellA2.Style.PatternColor = DoColor.FromHtml("#AF2F4F") ' Corrected color hex code

		' Save the workbook as 'set_background_pattern.xlsx'
		workbook.SaveAs("set_background_pattern.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • The above C# code demonstrates how to create an Excel workbook and manipulate cell styles using IronXL.
  • We start by creating a new workbook and accessing a worksheet within it.
  • For cell A1, we set an alternating bars pattern with an aquamarine color using BackgroundPattern.AlternatingBars.
  • For cell A2, we apply thick vertical bands with a custom color created from a HTML hex code.
  • Finally, the workbook is saved, which will reflect the changes made to the cell styles when opened in Excel.
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 Read Excel File in Blazor NET
NEXT >
How to Auto Resize Rows and Columns in Excel