如何在 C# 中使用 IronXL 设置 Excel 单元格字体大小

How to Set Cell Font and Size

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Change a Cell's Font Size in One Line

Use IronXL to instantly adjust the font size of a cell with minimal setup. This concise code shows how developers can pick a target cell and set its font height in a single, easy-to-read line using IronXL.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    workSheet["C3"].Style.Font.Height = 18;
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


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.

请注意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.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")
$vbLabelText   $csharpLabel
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.

: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")
$vbLabelText   $csharpLabel
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)
$vbLabelText   $csharpLabel

常见问题解答

如何在Excel电子表格中使用C#更改字体大小?

在IronXL中,您可以通过访问Font对象的Height属性来更改字体大小。例如,将cell.Style.Font.Height设置为您想要的大小。

IronXL中可用的字体自定义选项有哪些?

IronXL允许自定义字体属性,如名称、大小、颜色、加粗、斜体、下划线、删除线和脚本(上标或下标)。

如何在Excel单元格中使用C#加粗文本?

要加粗文本,将Font对象的Bold属性设置为true。例如,在IronXL中使用cell.Style.Font.Bold = true

是否可以在Excel中使用C#应用斜体和删除线样式到文本?

是的,您可以通过设置cell.Style.Font.Italic = true应用斜体,通过设置cell.Style.Font.Strikeout = true应用删除线,在IronXL中。

如何在Excel单元格中使用IronXL下划线文本?

使用Font对象的Underline属性应用下划线。例如,使用cell.Style.Font.Underline = ExcelFont.UnderlineType.Single

IronXL中可用的字体脚本选项有哪些?

IronXL提供三种字体脚本选项:none(默认)、super(用于上标)、sub(用于下标)。

如何在Excel单元格中使用C#更改字体颜色?

您可以使用IronXL的Color属性或SetColor方法更改字体颜色。例如,使用cell.Style.Font.Color = Color.Redcell.Style.Font.SetColor("#FF0000")

什么是Excel中的会计下划线?

会计下划线是一种下划线类型,包含额外的间距,通常用于数值,延伸超过文本项。

如何使用IronXL加载和保存Excel文件?

在IronXL中加载Excel文件,使用WorkBook.Load("example.xlsx")。要保存更改,使用workbook.SaveAs("example_modified.xlsx")

我可以在Excel单元格中使用C#设置特定的字体系列吗?

是的,您可以通过将所需的字体名称赋值给cell.Style.Font.Name来设置特定的字体系列,例如'Arial'或'Times New Roman'在IronXL中。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布