using C# 在 Excel 中設定儲存格樣式、邊框與字型
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");
NuGet 下載
PM > Install-Package IronXL.Excel
using C# 在 Excel 中設定儲存格樣式、邊框與字型

using C# 在 Excel 中設定儲存格樣式、邊框與字型

IronXL允許C#開發人員在C#中設計任何Excel Cell 和 Range。 IronXL 涵蓋了 Microsoft Excel 中大部分可用的樣式,例如字體、文字對齊方式、邊框、顏色、背景和圖案。 下面的程式碼範例展示了 IronXL 可配置的各種樣式。

單獨的Cell 和 Range 邊框(底部、頂部、左側、右側和對角線)可以設置為14種不同的邊框樣式,如虛線、破折號和雙線等,使用枚舉BorderType。 您可以啟用或禁用ShrinkToFit 和 WrapText 屬性,以更好地控制試算表如何組織儲存格大小。

學習如何在 C# 中調整 Excel 儲存格字體大小
在 GitHub 查看

準備好開始了嗎?

NuGet 下載次數 2,393,101版本:2026.11剛剛發布