IRONSOFTWAREHOME
Styliser les cellules, les bordures, et les polices dans Excel avec C#
using IronSoftware.Drawing;
using IronXL;
using IronXL.Styles;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

var range = workSheet["A1:H10"];

var cell = range.First();

// Set background color of the cell with an rgb string
cell.Style.SetBackgroundColor("#428D65");

// Apply styling to the whole range.

// Set underline property to the font
// FontUnderlineType is enum that stands for different types of font underlying
range.Style.Font.Underline = FontUnderlineType.SingleAccounting;

// Define whether to use horizontal line through the text or not
range.Style.Font.Strikeout = false;

// Define whether the font is bold or not
range.Style.Font.Bold = true;

// Define whether the font is italic or not
range.Style.Font.Italic = false;

// Get or set script property of a font
// Font script enum stands for available options
range.Style.Font.FontScript = FontScript.Super;

// Set the type of the border line
// There are also TopBorder,LeftBorder,RightBorder,DiagonalBorder properties
// BorderType enum indicates the line style of a border in a cell
range.Style.BottomBorder.Type = BorderType.MediumDashed;

// Indicate whether the cell should be auto-sized
range.Style.ShrinkToFit = true;

// Set alignment of the cell
range.Style.VerticalAlignment = VerticalAlignment.Bottom;

// Set border color
range.Style.DiagonalBorder.SetColor("#20C96F");

// Define border type and border direction as well
range.Style.DiagonalBorder.Type = BorderType.Thick;

// DiagonalBorderDirection enum stands for direction of diagonal border inside cell
range.Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward;

// Set background color of cells
range.Style.SetBackgroundColor(Color.Aquamarine);

// Set fill pattern of the cell
// FillPattern enum indicates the style of fill pattern
range.Style.FillPattern = FillPattern.Diamonds;

// Set the number of spaces to intend the text
range.Style.Indention = 5;

// Indicate if the text is wrapped
range.Style.WrapText = true;

workBook.SaveAs("stylingOptions.xls");
Imports IronSoftware.Drawing
Imports IronXL
Imports IronXL.Styles
Imports System.Linq

Dim workBook As WorkBook = WorkBook.Load("sample.xls")
Dim workSheet As WorkSheet = workBook.WorkSheets.First()

Dim range = workSheet("A1:H10")

Dim cell = range.First()

' Set background color of the cell with an rgb string
cell.Style.SetBackgroundColor("#428D65")

' Apply styling to the whole range.

' Set underline property to the font
' FontUnderlineType is enum that stands for different types of font underlying
range.Style.Font.Underline = FontUnderlineType.SingleAccounting

' Define whether to use horizontal line through the text or not
range.Style.Font.Strikeout = False

' Define whether the font is bold or not
range.Style.Font.Bold = True

' Define whether the font is italic or not
range.Style.Font.Italic = False

' Get or set script property of a font
' Font script enum stands for available options
range.Style.Font.FontScript = FontScript.Super

' Set the type of the border line
' There are also TopBorder,LeftBorder,RightBorder,DiagonalBorder properties
' BorderType enum indicates the line style of a border in a cell
range.Style.BottomBorder.Type = BorderType.MediumDashed

' Indicate whether the cell should be auto-sized
range.Style.ShrinkToFit = True

' Set alignment of the cell
range.Style.VerticalAlignment = VerticalAlignment.Bottom

' Set border color
range.Style.DiagonalBorder.SetColor("#20C96F")

' Define border type and border direction as well
range.Style.DiagonalBorder.Type = BorderType.Thick

' DiagonalBorderDirection enum stands for direction of diagonal border inside cell
range.Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward

' Set background color of cells
range.Style.SetBackgroundColor(Color.Aquamarine)

' Set fill pattern of the cell
' FillPattern enum indicates the style of fill pattern
range.Style.FillPattern = FillPattern.Diamonds

' Set the number of spaces to intend the text
range.Style.Indention = 5

' Indicate if the text is wrapped
range.Style.WrapText = True

workBook.SaveAs("stylingOptions.xls")
NuGet Download
PM > Install-Package IronXL.Excel
Styliser les cellules, les bordures, et les polices dans Excel avec C#

Styliser les cellules, les bordures, et les polices dans Excel avec C#

IronXL permet aux développeurs C# de styliser toute Cell et Range Excel en C#. IronXL prend en charge la plupart des options de style disponibles dans Microsoft Excel, telles que les polices, l'alignement du texte, les bordures, les couleurs, les arrière-plans et les motifs. L'exemple de code ci-dessous illustre un large éventail de styles configurables avec IronXL.

Les bordures individuelles Cell et Range (bas, haut, gauche, droite, et diagonale) peuvent être définies sur 14 styles de bordures différents tels que pointillés, tirets, et doubles, pour n'en nommer que quelques-uns, en utilisant l'enum BorderType. Vous pouvez activer ou désactiver les propriétés ShrinkToFit et WrapText pour avoir plus de contrôle sur la façon dont la feuille de calcul organise les tailles de cellules.

Apprenez à ajuster la taille de la police des cellules Excel en C#
Voir sur GitHub

Prêt à commencer?

Nuget Downloads 2,237,574Version :2026.9vient de sortir

Key in blue circle

Obtenez votre clé d'essai de 30 jours instantanément.

Your trial license will be sent to your email address

Aucune restriction. 100 % débloqué. Pas de carte bancaire.

bullet_checkedAucune carte de crédit ou création de compte requiseAucune restriction. 100 % débloqué. Pas de carte bancaire.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Réservez votre Démonstration en direct gratuite
Booking Badge

De confiance par des millions d'ingénieurs dans le monde entier

Logos des clients d'Iron Software
Obtenez Votre Consultation sans Engagement
Remplissez le formulaire ci-dessous ou envoyez un email à sales@ironsoftware.com
Vos informations seront toujours gardées confidentielles.
De confiance par des millions d'ingénieurs dans le monde entier
Logos des clients d'Iron Software
Obtenez votre clé d'essai 30 jours gratuitement.
Aucune carte de crédit ou création de compte requise