IRONSOFTWAREHOME
Style Cells, Borders, and Fonts in Excel with 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
Style Cells, Borders, and Fonts in Excel with C#

Style Cells, Borders, and Fonts in Excel with C#

IronXLは、C# 開発者が C# で任意のExcel Cell および Range のスタイル設定を可能にします。 IronXLは、Microsoft Excelで利用可能なスタイリングのほとんどをカバーしており、フォント、テキストの配置、境界線、色、背景、パターンなどがあります。 以下のコード例は、IronXLで設定可能な広範囲なスタイルを示しています。

個々の Cell および Range ボーダー(下、上、左、右、対角線)は、列挙型 BorderType を使用して、点線、破線、二重線など14種類のボーダースタイルに設定できます。 スプレッドシートがセルサイズを整理する方法をより制御するために、ShrinkToFit および WrapText プロパティを有効または無効にすることができます。

C#でExcelセルのフォントサイズを調整する方法を学ぶ
GitHubで見る

準備はできましたか?

Nuget Downloads 2,237,574バージョン:2026.9リリースされたばかり

Key in blue circle

無料の30日間トライアルキーをすぐに入手してください。

Your trial license will be sent to your email address

制限なし。100% ロック解除済み。クレジットカード不要。

bullet_checkedクレジットカードやアカウントの作成は不要です。制限なし。100% ロック解除済み。クレジットカード不要。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
無料のライブデモを予約する
Booking Badge

世界中の数百万人のエンジニアから信頼されています。

ライセンスはより安く
義務のない相談を受ける
下記のフォームを記入するか、sales@ironsoftware.comにメールしてください。
あなたの詳細は常に守秘されます。
世界中の数百万人のエンジニアから信頼されています。
ライセンスはより安く
あなたの無料30日間の試用キーをすぐに入手。
クレジットカードやアカウントの作成は不要です。