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
This article will discuss the C# Excel library that can be used to manage and manipulate Excel files using C#.
IronXL is a popular C# library that provides a comprehensive set of features for working with Excel files. It allows you to read, write, and manipulate Excel spreadsheets using a simple and intuitive API. IronXL supports various formatting options, including setting the background color of cells and also font color.
In the world of software development, working with Excel files is a common requirement, whether it's for data processing, reporting, or data analysis. To facilitate Excel manipulation in C# applications, the IronXL library comes to the rescue.
IronXL provides developers with a comprehensive set of APIs and functionalities to interact with Excel workbooks, worksheets, cells, formulas, styles, and more. It leverages the capabilities of the Office Open XML (OOXML) format, which is the default file format for Excel since Microsoft Office 2007. This means that IronXL is compatible with a wide range of Excel versions, making it a versatile choice for Excel automation tasks.
With IronXL, you can effortlessly create, edit, and manipulate Excel files programmatically using C#. It offers an intuitive and straightforward API that simplifies complex tasks such as reading data from multiple worksheets, writing data to specific cells, applying formatting and styling, performing calculations, and even generating charts.
Prerequisites for working with IronXL:
To work with IronXL, first, you need to create a new project in Visual Studio.
Open Visual Studio, go to Files and click on New Project.
Create a new project in Visual Studio
A new window will appear, in this new window select "Console Application" and click on the Next button.
Select a Console Application for the new project
Your new project in Visual Studio is created.
IronXL offers many ways to install this library, but those are the most effective approaches:
To add the IronXL library to your Visual Studio project using the NuGet Package Manager, search for IronXL in the Browse tab:
After finding IronXL in the search results, simply choose the package and initiate the installation process. Once the installation is finished, you can begin utilizing the IronXL library within your project.
The following screenshot demonstrates the process of accessing the NuGet Package Manager within Visual Studio.
Navigate to NuGet Package Manager
IronXL in search results:
Search and install the IronXL package in NuGet Package Manager UI
A popular method among developers for package installation is through the command line interface. To install IronXL using the command line, follow these steps:
Enter the following command in the console:
Install-Package IronXL
Install-Package IronXL
Once the process is complete, you can start utilizing the IronXL library in your project.
Install the IronXL package in the Package Manager Console
IronXL is the best choice when it comes to manipulating Excel files and changing its cell's background color. This section will explain how you can easily set the Excel Cell background color in an Excel Document. First, we need a sample Excel document to change its cell color.
a sample Excel file
The following source code will show how to do so.
using IronXL;
using IronXL.Styles;
using System.Linq;
// Load an existing Excel workbook
WorkBook workBook = WorkBook.Load("datatable.xlsx");
// Accesses the first worksheet from the workbook
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a range of cells in the worksheet (A1 to A10)
var range = workSheet["A1:A10"];
// Select the first cell in the defined range
var cell = range.First();
// Set background color of the selected cell using an RGB color string
cell.Style.SetBackgroundColor("#428D65");
// Save the modified workbook to a new file
workBook.SaveAs("stylingOptions3.xlsx");
using IronXL;
using IronXL.Styles;
using System.Linq;
// Load an existing Excel workbook
WorkBook workBook = WorkBook.Load("datatable.xlsx");
// Accesses the first worksheet from the workbook
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a range of cells in the worksheet (A1 to A10)
var range = workSheet["A1:A10"];
// Select the first cell in the defined range
var cell = range.First();
// Set background color of the selected cell using an RGB color string
cell.Style.SetBackgroundColor("#428D65");
// Save the modified workbook to a new file
workBook.SaveAs("stylingOptions3.xlsx");
Imports IronXL
Imports IronXL.Styles
Imports System.Linq
' Load an existing Excel workbook
Private workBook As WorkBook = WorkBook.Load("datatable.xlsx")
' Accesses the first worksheet from the workbook
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Define a range of cells in the worksheet (A1 to A10)
Private range = workSheet("A1:A10")
' Select the first cell in the defined range
Private cell = range.First()
' Set background color of the selected cell using an RGB color string
cell.Style.SetBackgroundColor("#428D65")
' Save the modified workbook to a new file
workBook.SaveAs("stylingOptions3.xlsx")
The above code example opens an Excel file using the WorkBook.Load
method. Then, it loads the first worksheet using the workBook.WorkSheets.First()
method. Next, the SetBackgroundColor
method adds color to the selected cell. Finally, save the Excel WorkBook
file using WorkBook.SaveAs
.
Here is an output screenshot of the above source code example.
The formatted Excel file
As you can see, the background color of cell A1 in column A is changed to green.
Setting the background color of Excel cells using C# can be achieved with the help of libraries like IronXL. IronXL provides a comprehensive set of APIs and functionalities to interact with Excel files, allowing you to read, write, and manipulate Excel spreadsheets programmatically. By using IronXL, you can easily set the background color of cells in an Excel file by specifying the RGB color code. This can be beneficial for improving readability and highlighting important information in your Excel spreadsheets.
With its intuitive API and compatibility with various Excel versions, IronXL is a powerful and versatile choice for Excel automation tasks in C#.
You can get a related tutorial at the following Excel cell formatting tutorial. For more information on IronXL, visit another read Excel file tutorial. Visit the API reference page to know more about the class calls of the interface IStyle
.
IronXL is available to users for a free trial and can be licensed for commercial use with its Lite package starting from $749 only.
IronXL is a popular C# library that provides a comprehensive set of features for working with Excel files. It allows you to read, write, and manipulate Excel spreadsheets using a simple and intuitive API, including setting the background color of cells.
To work with IronXL, you need a development environment like Visual Studio, basic knowledge of C#, the IronXL library installed in your project, and an Excel file to modify.
To install IronXL using NuGet Package Manager, search for IronXL in the Browse tab of the NuGet Package Manager in Visual Studio, select the package, and initiate the installation process.
Yes, to install IronXL using the Visual Studio Command Line, navigate to Tools > NuGet Package Manager > Package Manager Console, and enter the command 'Install-Package IronXL' to initiate the download and installation.
You can set the background color of an Excel cell using IronXL by defining a range of cells, selecting the specific cell, and using the SetBackgroundColor method with an RGB color string.
IronXL is compatible with a wide range of Excel versions as it leverages the capabilities of the Office Open XML (OOXML) format, the default file format for Excel since Microsoft Office 2007.
IronXL provides an intuitive API that simplifies complex tasks like reading, writing, and formatting Excel files, making it a powerful choice for Excel automation tasks in C#.
Yes, IronXL is available for a free trial, and it can be licensed for commercial use with its Lite package.
You can find more tutorials, like the Excel cell formatting tutorial, on the IronXL website, along with API reference pages to explore class calls further.