如何设置 Excel 中的单元格边框和对齐 | IronXL

How to Set Cell Border and Alignment

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

In Excel, cell borders are lines or borders that can be applied to individual cells or groups of cells, while text alignment refers to the positioning of text within a cell both vertically and horizontally.

Quickstart: Set Cell Borders & Center Text Alignment with One Call

Here’s how easy it is to format a cell in IronXL: apply a border style to one side and center the text horizontally—all in just two simple lines. Get up and running fast without Interop overhead.

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["B2"].Style.LeftBorder.Type = IronXL.Styles.BorderType.MediumDashed;
    workSheet["B2"].Style.HorizontalAlignment = IronXL.Styles.HorizontalAlignment.Center;
  3. Deploy to test on your live environment

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


Get started with IronXL

今天在您的项目中使用 IronXL,免费试用。

第一步:
green arrow pointer


Set Cell Border and Alignment Example

Customize the appearance of a selected cell, column, row, or range by adding a border using the TopBorder, RightBorder, BottomBorder, and LeftBorder properties. Choose from various styles available in the IronXL.Styles.BorderType enum. Explore all available border types to find the perfect match.

For precise text alignment, adjust the HorizontalAlignment and VerticalAlignment properties in Style to achieve the desired layout. Utilize the IronXL.Styles.HorizontalAlignment and IronXL.Styles.VerticalAlignment enum to set the desired alignment. Discover all available alignment types to present your data flawlessly.

:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-alignment.cs
using IronXL;
using IronXL.Styles;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

workSheet["B2"].Value = "B2";

// Set cell border
workSheet["B2"].Style.LeftBorder.Type = BorderType.MediumDashed;
workSheet["B2"].Style.RightBorder.Type = BorderType.MediumDashed;

// Set text alignment
workSheet["B2"].Style.HorizontalAlignment = HorizontalAlignment.Center;

workBook.SaveAs("setBorderAndAlignment.xlsx");
Imports IronXL
Imports IronXL.Styles

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private workSheet("B2").Value = "B2"

' Set cell border
Private workSheet("B2").Style.LeftBorder.Type = BorderType.MediumDashed
Private workSheet("B2").Style.RightBorder.Type = BorderType.MediumDashed

' Set text alignment
Private workSheet("B2").Style.HorizontalAlignment = HorizontalAlignment.Center

workBook.SaveAs("setBorderAndAlignment.xlsx")
$vbLabelText   $csharpLabel
Border And Alignment

Set Cell Border and Alignment Advanced Example

Border Color

By default, the border color is black, but you can customize it to any color available in the Color class or use a Hex color code. To set the border color, you can use the Color property with the desired color or Hex code. Additionally, the Color property allows you to retrieve the color of the border.

请注意Setting the border color alone will not display any effect unless the border type has also been set to one of the available types.

:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-color.cs
using IronXL;
using IronXL.Styles;
using IronSoftware.Drawing;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

workSheet["B2"].Style.LeftBorder.Type = BorderType.Thick;
workSheet["B2"].Style.RightBorder.Type = BorderType.Thick;

// Set cell border color
workSheet["B2"].Style.LeftBorder.SetColor(Color.Aquamarine);
workSheet["B2"].Style.RightBorder.SetColor("#FF7F50");

workBook.SaveAs("setBorderColor.xlsx");
Imports IronXL
Imports IronXL.Styles
Imports IronSoftware.Drawing

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private workSheet("B2").Style.LeftBorder.Type = BorderType.Thick
Private workSheet("B2").Style.RightBorder.Type = BorderType.Thick

' Set cell border color
workSheet("B2").Style.LeftBorder.SetColor(Color.Aquamarine)
workSheet("B2").Style.RightBorder.SetColor("#FF7F50")

workBook.SaveAs("setBorderColor.xlsx")
$vbLabelText   $csharpLabel
Border Color

Border Lines & Patterns

In total, there are six border line positions, each offering a variety of patterns or types. These positions include top, right, bottom, left, as well as diagonal lines moving forward, backward, and both.

:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-line.cs
using IronXL;
using IronXL.Styles;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

workSheet["B2"].StringValue = "Top";
workSheet["B4"].StringValue = "Forward";

// Set top border line
workSheet["B2"].Style.TopBorder.Type = BorderType.Thick;

// Set diagonal border line
workSheet["B4"].Style.DiagonalBorder.Type = BorderType.Thick;
// Set diagonal border direction
workSheet["B4"].Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward;

workBook.SaveAs("borderLines.xlsx");
Imports IronXL
Imports IronXL.Styles

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private workSheet("B2").StringValue = "Top"
Private workSheet("B4").StringValue = "Forward"

' Set top border line
Private workSheet("B2").Style.TopBorder.Type = BorderType.Thick

' Set diagonal border line
Private workSheet("B4").Style.DiagonalBorder.Type = BorderType.Thick
' Set diagonal border direction
Private workSheet("B4").Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward

workBook.SaveAs("borderLines.xlsx")
$vbLabelText   $csharpLabel

Border Lines

Available Border Lines

Border Patterns

Available Border Types

Alignment Types

Discover the full range of alignment options offered by IronXL in the illustration below:

Available Alignment Types

HorizontalAlignment Enumeration

  • General: General-aligned horizontal alignment. Text data is left-aligned. Numbers, dates, and times are right-aligned, and Boolean types are centered. Changing the alignment does not affect the data type.
  • Left: Left-aligned horizontal alignment, even in Right-to-Left mode. Aligns contents at the left edge of the cell. If an indent amount is specified, the cell's contents are indented from the left by the specified number of character spaces.
  • Center: Centered horizontal alignment, meaning the text is centered across the cell.
  • Right: Right-aligned horizontal alignment, meaning that cell contents are aligned at the right edge of the cell, even in Right-to-Left mode.
  • Fill: The value of the cell is filled across the entire width. If adjacent cells to the right also have the same fill alignment, they will be filled as well. Additional rules:
    • Only whole values can be appended, not partial values.
    • The column will not be widened to 'best fit' the filled value.
    • If appending an additional occurrence of the value exceeds the boundary of the cell's left/right edge, it will not be added.
    • The display value of the cell is filled, not the underlying raw number.
  • Justify: Justified (flush left and right) horizontal alignment. Applies wrap text to the cell and ensures that each line aligns the first word with the left edge and the last word with the right edge of the cell (except for the last line).
  • CenterSelection: Horizontally centers the content of the left-most cell to the center across multiple cells. It visually appears similar to merging cells, but without actually merging them. Using this option helps prevent potential issues that may arise from merged cells.
  • Distributed: Each 'word' in each line of text inside the cell is evenly distributed across the cell's width, with flush right and left margins. If there is an indent value to apply, both the left and right sides of the cell are padded by the indent value.

VerticalAlignment Enumeration

  • None: The default alignment.
  • Top: Aligns content at the top of the cell.
  • Center: Vertically centers the content within the cell.
  • Bottom: Aligns content at the bottom of the cell.
  • Justify: Distributes the lines of text evenly across the cell's height, with flush top and bottom margins. Works similarly to horizontal justification by wrapping text and adjusting the spaces between lines to occupy the entire row's height.
  • Distributed: Distributes each 'word' in each line of text evenly across the cell's height, with flush top and bottom margins in horizontal text direction. In vertical text direction, it behaves exactly as distributed - horizontal alignment, evenly distributing the lines of text from top to bottom.

常见问题解答

如何使用 C# 在 Excel 中设置单元格边框?

要在 Excel 中使用 C# 设置单元格边框,可以使用 IronXL 的属性,如 TopBorderRightBorderBottomBorderLeftBorder。使用 Color 类或十六进制代码自定义每一边的不同 BorderType 和颜色。

使用 C# 在 Excel 单元格中对齐文本有哪些方法?

IronXL 允许您通过使用 HorizontalAlignmentVerticalAlignment 属性设置 Excel 单元格中的文本对齐。您可以从选项中选择 LeftCenterRightTopCenterBottom 来实现精确的数据呈现。

如何使用 C# 将不同的边框样式应用于 Excel 单元格?

IronXL 提供一系列边框样式,如 ThinDoubleDashedDottedSolid。您可以将这些样式应用于单元格的每一边,以增强电子表格的外观。

能否解释如何使用 C# 库自定义 Excel 中的边框颜色?

是的,使用 IronXL,您可以通过使用十六进制颜色代码或预定义颜色类型设置 Color 属性轻松自定义边框颜色,全面掌控电子表格的美学。

在 C# 中自定义 Excel 工作簿后保存的流程是什么?

在使用 IronXL 自定义单元格边框和对齐后,您可以使用 SaveAs 方法保存工作簿,指定所需的文件名和格式,确保更改得到保存。

在 C# Excel 库中,'Fill' 对齐如何工作?

在 IronXL 中,'Fill' 对齐会将单元格内容拉伸以填满单元格的整个宽度。如果相邻单元格也具有 'Fill' 对齐,内容将延伸到那些单元格,创建无缝外观。

使用 IronXL 进行 Excel 操作在 C# 中有什么优势?

在 C# 中使用 IronXL 进行 Excel 操作提供了许多优势,包括增强的数据可视化、更好的可读性,以及无需依赖 Excel Interop 即可创建专业外观的电子表格。

在使用 C# 为 Excel 设置边框时,如何排除常见问题?

常见问题可能包括边框应用不当或颜色不匹配。确保正确设置 BorderTypeColor 属性。使用 IronXL 时,请仔细检查您的十六进制代码和属性分配。

什么是 'Justify' 垂直对齐,以及如何在 C# 中使用?

IronXL 中的 'Justify' 垂直对齐将文本均匀分布在单元格的整个高度,并具有上下边距,类似于水平对齐,提供整齐且平衡的外观。

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