How to Set Cell Font and Size

by Chaknith Bin

Customizing font properties, including font name, size, color, underline, bold, italic, script, and strikeout, offers numerous benefits in document formatting. These options empower you to improve readability, emphasize critical information, and create visually appealing documents. With IronXL, you can effortlessly edit font properties without interop in C# .NET, simplifying the process and enabling you to create professional and polished materials effortlessly.


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Set Cell Font and Size Example

To personalize the font of a selected cell, column, row, or range, simply set the Font properties of the Style. Utilize the Name property to set the desired font family, the Height property to adjust the font size, and the Bold property to emphasize the font weight. Additionally, you can employ the Underline property to add underlining for further visual emphasis.

:path=/static-assets/excel/content-code-examples/how-to/cell-font-size-set-font.cs
using IronXL;
using IronXL.Styles;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

workSheet["B2"].StringValue = "Font and Size";

// Set font family
workSheet["B2"].Style.Font.Name = "Times New Roman";

// Set font size
workSheet["B2"].Style.Font.Height = 15;

// Set font to bold
workSheet["B2"].Style.Font.Bold = true;

// Set underline
workSheet["B2"].Style.Font.Underline = FontUnderlineType.Single;

workBook.SaveAs("fontAndSize.xlsx");
Imports IronXL
Imports IronXL.Styles

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private workSheet("B2").StringValue = "Font and Size"

' Set font family
Private workSheet("B2").Style.Font.Name = "Times New Roman"

' Set font size
Private workSheet("B2").Style.Font.Height = 15

' Set font to bold
Private workSheet("B2").Style.Font.Bold = True

' Set underline
Private workSheet("B2").Style.Font.Underline = FontUnderlineType.Single

workBook.SaveAs("fontAndSize.xlsx")
VB   C#
Set Font And Size

Set Cell Font and Size Advanced Example

In addition to the options discussed in the previous section, you can further customize the font appearance in Excel. This includes setting the font to Italic, applying Strikeout, using FontScript for super and subscripts, and choosing a specific font color. Below is an example demonstrating how to utilize these additional options to create personalized font styles for your cells.

Please note
The Name property sets the font name exactly as provided. For instance, if you want to use the "Times New Roman" font, make sure to input it exactly with the same spaces and capital letters.

:path=/static-assets/excel/content-code-examples/how-to/cell-font-size-set-font-advanced.cs
using IronXL;
using IronXL.Styles;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

workSheet["B2"].StringValue = "Advanced";

// Set font family
workSheet["B2"].Style.Font.Name = "Lucida Handwriting";

// Set font script
workSheet["B2"].Style.Font.FontScript = FontScript.None;

// Set underline
workSheet["B2"].Style.Font.Underline = FontUnderlineType.Double;

// Set bold property
workSheet["B2"].Style.Font.Bold = true;

// Set italic property
workSheet["B2"].Style.Font.Italic = false;

// Set strikeout property
workSheet["B2"].Style.Font.Strikeout = false;

// Set font color
workSheet["B2"].Style.Font.Color = "#00FFFF";

workBook.SaveAs("fontAndSizeAdvanced.xlsx");
Imports IronXL
Imports IronXL.Styles

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private workSheet("B2").StringValue = "Advanced"

' Set font family
Private workSheet("B2").Style.Font.Name = "Lucida Handwriting"

' Set font script
Private workSheet("B2").Style.Font.FontScript = FontScript.None

' Set underline
Private workSheet("B2").Style.Font.Underline = FontUnderlineType.Double

' Set bold property
Private workSheet("B2").Style.Font.Bold = True

' Set italic property
Private workSheet("B2").Style.Font.Italic = False

' Set strikeout property
Private workSheet("B2").Style.Font.Strikeout = False

' Set font color
Private workSheet("B2").Style.Font.Color = "#00FFFF"

workBook.SaveAs("fontAndSizeAdvanced.xlsx")
VB   C#
Set Font And Size advanced

Underline

In Excel, there are different types of underlines available for text formatting. One such option is the Accounting underline, which has additional spacing between the characters and the lines compared to the normal underline. For text entries, the underline extends beyond the value both in front and back. However, for numeric data formats, the underline remains confined to the value. In cases where a cell contains both numbers and other characters, the Accounting underline will behave similarly to text.

Available Underline Options

Font Script

Font script in IronXL offers three options: none, super, and sub.

  • none: The default option, setting the font on the baseline for regular text appearance.
  • super: Positions text characters above the baseline for exponents or footnotes.
  • sub: Positions text characters below the baseline for chemical formulas and math notations.
Available Font Script Options

Font Color

You can set the font color using either the Color property or the SetColor method. The SetColor method accepts input in the form of IronSoftware.Drawing.Color or a Hex color code.

:path=/static-assets/excel/content-code-examples/how-to/cell-font-size-set-font-color.cs
using IronXL;
using IronSoftware.Drawing;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set Color property
workSheet["B2"].Style.Font.Color = "#00FFFF";

// Use Hex color code
workSheet["B2"].Style.Font.SetColor("#00FFFF");

// Use IronSoftware.Drawing
workSheet["B2"].Style.Font.SetColor(Color.Red);
Imports IronXL
Imports IronSoftware.Drawing

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set Color property
Private workSheet("B2").Style.Font.Color = "#00FFFF"

' Use Hex color code
workSheet("B2").Style.Font.SetColor("#00FFFF")

' Use IronSoftware.Drawing
workSheet("B2").Style.Font.SetColor(Color.Red)
VB   C#

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.