Comment définir la taille de la police des cellules dans Excel en utilisant C# | IronXL

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.

Veuillez noterThe 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

Questions Fréquemment Posées

Comment puis-je changer la taille de la police dans une feuille de calcul Excel en utilisant C# ?

Dans IronXL, vous pouvez changer la taille de la police en accédant à la propriété Height de l'objet Font. Par exemple, définissez cell.Style.Font.Height à la taille souhaitée.

Quelles sont les options de personnalisation de police disponibles dans IronXL ?

IronXL permet la personnalisation des propriétés de la police telles que le nom, la taille, la couleur, la graisse, l'italique, le soulignement, le barré et le script (exposant ou indice).

Comment rendre le texte en gras dans une cellule Excel en utilisant C# ?

Pour rendre le texte en gras, définissez la propriété Bold de l'objet Font sur true. Par exemple, utilisez cell.Style.Font.Bold = true dans IronXL.

Est-il possible d'appliquer les styles italique et barré au texte dans Excel avec C# ?

Oui, vous pouvez appliquer l'italique en définissant cell.Style.Font.Italic = true et le barré en définissant cell.Style.Font.Strikeout = true dans IronXL.

Comment puis-je souligner du texte dans une cellule Excel en utilisant IronXL ?

Utilisez la propriété Underline de l'objet Font pour appliquer le soulignement. Par exemple, utilisez cell.Style.Font.Underline = ExcelFont.UnderlineType.Single.

Quelles options de script de police sont disponibles dans IronXL ?

IronXL offre trois options de script de police : none (par défaut), super (pour exposant) et sub (pour indice).

Comment changer la couleur de police dans une cellule Excel en utilisant C# ?

Vous pouvez changer la couleur de la police dans IronXL en utilisant la propriété Color ou la méthode SetColor. Par exemple, utilisez cell.Style.Font.Color = Color.Red ou cell.Style.Font.SetColor('#FF0000').

Qu'est-ce que le soulignement comptable dans Excel ?

Le soulignement comptable est un type de soulignement qui comprend un espacement supplémentaire et est généralement utilisé avec des valeurs numériques, s'étendant au-delà des entrées de texte.

Comment charger et enregistrer un fichier Excel en utilisant IronXL ?

Pour charger un fichier Excel dans IronXL, utilisez WorkBook.Load('example.xlsx'). Pour enregistrer les modifications, utilisez workbook.SaveAs('example_modified.xlsx').

Puis-je définir une famille de polices spécifique dans une cellule Excel en utilisant C# ?

Oui, vous pouvez définir une famille de polices spécifique en assignant le nom de police désiré à cell.Style.Font.Name, comme 'Arial' ou 'Times New Roman' dans IronXL.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir