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
One of the most effective programs for manipulating and visualizing data is still Microsoft Excel. Its adaptability includes the ability to structure and customize data to meet individual needs, in addition to its extensive feature set. To improve data presentation and make it more legible and visually appealing, font styles are essential. A common way to manipulate font styles with Excel in C# is using the Font property utilizing Microsoft.Office.Interop.Excel. However, this option isn't as flexible or convenient as alternative solutions. Hence, this post will discuss using IronXL, on how to use C# Excel to set font style.
var workbook = WorkBook.Load("example.xlsx");
.var sheet = workbook.WorkSheets[0];
.var range = sheet["A1:B2"];
.range.Style.Font.Name = "Calibri";
, font size with range.Style.Font.Size = 12;
, color with range.Style.Font.Color = "#FF0000";
, and other properties like range.Style.Font.Bold = true;
and range.Style.Font.Italic = true;
.SaveAs()
method.Before diving into Excel font styles, let's quickly introduce IronXL. IronXL is a popular C# library that allows you to create, read, and manipulate Excel files within C# applications easily. It offers a user-friendly API for working with Excel files, making tasks like reading, writing, and formatting data significantly more accessible.
Developers can utilize IronXL to automate Excel-related processes using C#. For C# developers looking to interact with Excel files programmatically, its user-friendly interface and comprehensive documentation make it the go-to option.
Some of IronXL's key characteristics are listed below:
IronXL is a feature-rich and adaptable C# library for manipulating Excel that provides a multitude of options to help you simplify your activities connected to Excel. IronXL offers the features and tools you need to be successful, whether you're building data-driven applications, analyzing data, or producing reports. Refer to the documentation here to learn more.
To open the Visual Studio application, select File from the File menu. After selecting "New Project," select "Console application."
After choosing the file location, type the project name into the assigned text field. Subsequently, pick the required .NET Framework by clicking the Create button, as demonstrated in the sample below.
The Visual Studio project's organization will then depend on the selected application. To add code and construct the application, just open the program.cs file. The internet application, Windows, or console can all be used.
The code can then be tested and the library added.
The following patch requires the installation of the IronXL library. Finally, launch the NuGet Package Manager Console and use the following command to complete the installation.
Install-Package IronXL.Excel
An alternative would be to use the NuGet Package Manager to search for the package "IronXL". We may choose which of the NuGet packages associated with IronXL have to be downloaded from this list of all of them.
To begin, allow me to demonstrate how to set font characteristics for a range of cells in a basic way:
using IronXL;
class Program
{
static void Main(string [] args)
{
// Load the existing Excel workbook
WorkBook workbook = WorkBook.Load("Demo.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets[0];
// Define the range of cells
var range = sheet["A1:B2"];
// Set font characteristics for the range of cells
range.Style.Font.Name = "Calibri"; // Font name
range.Style.Font.Size = 12; // Font size
range.Style.Font.Color = "#FF0000"; // Font color
range.Style.Font.Bold = true; // Bold font
range.Style.Font.Italic = true; // Italicized font
// Save the modified workbook as a new file
workbook.SaveAs("Sample.xlsx");
}
}
using IronXL;
class Program
{
static void Main(string [] args)
{
// Load the existing Excel workbook
WorkBook workbook = WorkBook.Load("Demo.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets[0];
// Define the range of cells
var range = sheet["A1:B2"];
// Set font characteristics for the range of cells
range.Style.Font.Name = "Calibri"; // Font name
range.Style.Font.Size = 12; // Font size
range.Style.Font.Color = "#FF0000"; // Font color
range.Style.Font.Bold = true; // Bold font
range.Style.Font.Italic = true; // Italicized font
// Save the modified workbook as a new file
workbook.SaveAs("Sample.xlsx");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the existing Excel workbook
Dim workbook As WorkBook = WorkBook.Load("Demo.xlsx")
' Select the first worksheet
Dim sheet As WorkSheet = workbook.WorkSheets(0)
' Define the range of cells
Dim range = sheet("A1:B2")
' Set font characteristics for the range of cells
range.Style.Font.Name = "Calibri" ' Font name
range.Style.Font.Size = 12 ' Font size
range.Style.Font.Color = "#FF0000" ' Font color
range.Style.Font.Bold = True ' Bold font
range.Style.Font.Italic = True ' Italicized font
' Save the modified workbook as a new file
workbook.SaveAs("Sample.xlsx")
End Sub
End Class
The Excel file "Demo.xlsx" is being loaded into the WorkBook object first. It will create a new file with this name if the current one doesn't exist. In this case, we are choosing the workbook's first worksheet (WorkSheet). If the workbook has more than one sheet, you can select a particular sheet by name or index. Within the chosen Excel worksheet, we designate a range of cells from cell A1 to cell B2. This enables us to set font styles for this particular cell range.
For the chosen range of cells, these lines set the following font object properties:
Font.Name
: Defines the font type (Calibri).Font.Size
: Sets the font size.Font.Color
: Changes the font color to red (#FF0000 in hexadecimal).Font.Bold
: Makes the text bold.Font.Italic
: Apply italics to the text.Lastly, we save the altered worksheet to a new file called "Sample.xlsx" after applying the font styles. Below is the output generated from the above code.
An IFont interface that exposes the FontName field is given via the Style property. You can specify the preferred font name for the cell using this parameter.
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Name = "Calibri"; // Set font name
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Name = "Calibri"; // Set font name
' Select the range of cells
Dim fontrange = sheet("A1:B1")
fontrange.Style.Font.Name = "Calibri" ' Set font name
An IFont interface that exposes the Color property is given by the Style property. You can use a hex code string to set the font color with this property. Six hexadecimal digits are used to define colors in hex codes (e.g., #FF0000 for red, #00FFFF for cyan). To obtain the proper hex code for the color of your typeface, there are numerous color pickers available online.
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Color = "#FF0000"; // Set font color
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Color = "#FF0000"; // Set font color
' Select the range of cells
Dim fontrange = sheet("A1:B1")
fontrange.Style.Font.Color = "#FF0000" ' Set font color
The Bold property is exposed through the IFontFormatting interface that is provided by the Style property. The font's boldness is set by this attribute, which is a boolean value. To make the typeface bold in the selected cell, set the Bold property to true.
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Bold = true; // Set font to bold
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Bold = true; // Set font to bold
' Select the range of cells
Dim fontrange = sheet("A1:B1")
fontrange.Style.Font.Bold = True ' Set font to bold
The Italic property is exposed through the IFontFormatting interface that is provided by the Style property. This property, which is a boolean value, controls whether or not the typeface is italic. To make the font italic in the selected cell, set the Italic property to true.
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Italic = true; // Set font to italic
// Select the range of cells
var fontrange = sheet["A1:B1"];
fontrange.Style.Font.Italic = true; // Set font to italic
' Select the range of cells
Dim fontrange = sheet("A1:B1")
fontrange.Style.Font.Italic = True ' Set font to italic
The above list of codes shows you how to use IronXL to programmatically change the font styles in Excel using C#. You may quickly alter how text appears in Excel spreadsheets by loading an existing workbook, choosing a worksheet, specifying a range of cells, and adjusting font attributes. Although not shown above, you can also similarly modify the font size for a specific cell or the entire worksheet, for a list of attributes regarding the interface IFont refer to here, and to know more about the code example refer to here.
Excel spreadsheet readability and visual appeal are greatly improved by the use of font styles. Programmatically changing font styles becomes simple and scalable when using C# and IronXL. If you need to apply styles to specific cells or format large ranges of cells, whether you're creating a new workbook or editing an existing one, IronXL offers an extensive toolkit to fulfill your needs.
By mastering Excel font styles using C# and IronXL, you can create professional-looking spreadsheets that effectively communicate your data and insights. A refined and powerful user experience may be achieved by careful consideration of font styling, whether you're creating dashboards, reports, or data-driven applications.
When it comes to processing and presenting data, the options are endless. Having IronXL in your toolbox gives you the ability to manage a variety of activities linked to Excel in your C# programs. When ready to commit, users can start using IronXL for free. Check here to learn more about the Iron software product.
IronXL is a C# library that allows developers to create, read, and manipulate Excel files easily. It offers a user-friendly API for tasks such as setting font styles, making it a flexible alternative to Microsoft.Office.Interop.Excel for customizing Excel spreadsheets.
To install IronXL in your C# project, use NuGet Package Manager by running the command: Install-Package IronXL.Excel. Alternatively, you can search for 'IronXL' in the NuGet Package Manager and install it from there.
To set font style using IronXL, load an Excel workbook, select a worksheet, define a cell range, and then set font properties like name, size, color, bold, and italic using the range.Style.Font properties.
Yes, you can change the font color by setting the range.Style.Font.Color property to a hex color code, such as '#FF0000' for red, using IronXL in a C# application.
Yes, you can make text bold by setting range.Style.Font.Bold to true and italic by setting range.Style.Font.Italic to true for the specified cell range using IronXL.
IronXL offers a more flexible and convenient API for manipulating Excel files without the need for Interop. It simplifies tasks like setting font styles, reading data, writing to Excel, and more, making it a preferred choice for many C# developers.
After making changes to an Excel file using IronXL, save the modified workbook using the workbook.SaveAs("filename.xlsx") method to preserve the changes.
Using IronXL, you can modify a variety of font attributes such as font name, size, color, boldness, and italicization for a specified range of cells in an Excel worksheet.
Yes, IronXL integrates seamlessly with .NET applications, providing a user-friendly programming interface and extensive API support for manipulating Excel files within .NET projects.
Comprehensive documentation for IronXL can be found on the IronXL official website, providing detailed guides and examples for various features, including font styling in Excel.