How to Set Cell Borders and Alignment in Excel | IronXL

How to Set Cell Border & Alignment in C# with IronXL

Set cell borders and text alignment in C# Excel files using IronXL by applying border types like MediumDashed and alignment properties like HorizontalAlignment.Center directly to worksheet cells without Microsoft Interop dependencies.

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. IronXL provides a comprehensive API for working with Excel cell styles that allows you to programmatically control these visual elements with precision.

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

Apply a border style to one side and center text horizontally in just two 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.


How Do I Set Cell Borders and Text Alignment in a Basic Example?

Customize the appearance of a selected cell, column, row, or range by adding borders 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. Use the IronXL.Styles.HorizontalAlignment and IronXL.Styles.VerticalAlignment enums to set the desired alignment. Discover all available alignment types to present your data flawlessly. This approach is particularly useful when creating professional spreadsheets that require consistent formatting.

Before applying borders and alignment, ensure you have loaded your Excel workbook properly. Here's a complete example demonstrating the basic border and alignment setup:

: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");
$vbLabelText   $csharpLabel
Spreadsheet cell B2 with dotted borders applied, showing border formatting result in Excel-style grid

What Advanced Border and Alignment Options Are Available?

How Can I Customize Border Colors?

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, use the Color property with the desired color or Hex code. Additionally, the Color property allows you to retrieve the color of the border. This flexibility is similar to how you can set background colors for cells.

Please noteSetting the border color alone will not display any effect unless the border type has also been set to one of the available types.

The border color customization works seamlessly with other cell formatting features. When combined with conditional formatting, you can create dynamic visual cues that help users quickly identify important data patterns in your spreadsheets.

: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");
$vbLabelText   $csharpLabel
Spreadsheet showing cell B2 with green left border and cell C2 with orange right border demonstrating border colors

Which Border Line Positions and Patterns Can I Apply?

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. This comprehensive set of options allows you to create complex visual layouts similar to those you might achieve when working with merged cells, but with more flexibility.

When working with diagonal borders, understand how they interact with the cell's content and alignment settings. Diagonal borders can be particularly useful for creating header sections or visual separators in your Excel reports.

: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");
$vbLabelText   $csharpLabel

For more complex border scenarios, consider creating a reusable function that applies a consistent border style across multiple cells or ranges:

void ApplyUniformBorder(WorkSheet sheet, string range, BorderType borderType, Color borderColor)
{
    var cells = sheet[range];

    // Apply borders to all sides
    cells.Style.TopBorder.Type = borderType;
    cells.Style.RightBorder.Type = borderType;
    cells.Style.BottomBorder.Type = borderType;
    cells.Style.LeftBorder.Type = borderType;

    // Apply color to all borders
    cells.Style.TopBorder.SetColor(borderColor);
    cells.Style.RightBorder.SetColor(borderColor);
    cells.Style.BottomBorder.SetColor(borderColor);
    cells.Style.LeftBorder.SetColor(borderColor);
}

// Usage example
ApplyUniformBorder(workSheet, "A1:D4", BorderType.Thin, Color.Black);
void ApplyUniformBorder(WorkSheet sheet, string range, BorderType borderType, Color borderColor)
{
    var cells = sheet[range];

    // Apply borders to all sides
    cells.Style.TopBorder.Type = borderType;
    cells.Style.RightBorder.Type = borderType;
    cells.Style.BottomBorder.Type = borderType;
    cells.Style.LeftBorder.Type = borderType;

    // Apply color to all borders
    cells.Style.TopBorder.SetColor(borderColor);
    cells.Style.RightBorder.SetColor(borderColor);
    cells.Style.BottomBorder.SetColor(borderColor);
    cells.Style.LeftBorder.SetColor(borderColor);
}

// Usage example
ApplyUniformBorder(workSheet, "A1:D4", BorderType.Thin, Color.Black);
$vbLabelText   $csharpLabel

Border Lines

Excel border styles demo showing Top, Right, Bottom, Left borders and diagonal directions None, Forward, Backward

Border Patterns

Spreadsheet showing available cell border types from None to SlantedDashDot with visual examples

What Text Alignment Options Are Supported?

Text alignment in IronXL provides comprehensive control over how content is positioned within cells. This feature is particularly important when exporting data to different formats where maintaining visual consistency is crucial. The alignment system works in conjunction with other formatting features like font styling to create professional-looking spreadsheets.

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

Excel spreadsheet showing horizontal and vertical text alignment options with visual examples

HorizontalAlignment Enumeration

  • General: General-aligned horizontal alignment. Text data is left-aligned. Numbers, dates, and times are right-aligned. Boolean types are centered. Changing the alignment does not affect the data type. This is the default alignment when no specific alignment is set.
  • 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. The text is centered across the cell. This is commonly used for headers and titles.
  • Right: Right-aligned horizontal alignment. Cell contents are aligned at the right edge of the cell, even in Right-to-Left mode. This is typically used for numeric data presentation.
  • Fill: The cell value fills 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). This is particularly useful for text-heavy cells containing paragraphs.
  • CenterSelection: Horizontally centers the content of the leftmost 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, which typically results in bottom alignment behavior.
  • Top: Aligns content at the top of the cell. Useful for cells with varying content heights.
  • Center: Vertically centers the content within the cell. This creates a balanced appearance, especially when combined with horizontal centering.
  • Bottom: Aligns content at the bottom of the cell. This is the standard behavior for most spreadsheet applications.
  • 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.

When working with alignment settings, consider how they interact with other cell properties. For example, when you autosize rows and columns, the alignment settings will affect how the content fits within the automatically sized cells. Similarly, when working with formulas, proper alignment can make your spreadsheet calculations more readable and professional.

Frequently Asked Questions

How do I add borders to Excel cells in C# without Microsoft Office?

You can add borders to Excel cells using IronXL's Style API. Simply access the cell's Style property and set border types like MediumDashed using properties like LeftBorder.Type, RightBorder.Type, TopBorder.Type, and BottomBorder.Type. IronXL works independently without requiring Microsoft Office or Interop dependencies.

What border styles are available for Excel cells in C#?

IronXL provides various border styles through the IronXL.Styles.BorderType enum, including options like MediumDashed, Thin, Thick, Double, and more. You can apply these styles to any side of a cell (top, right, bottom, left) to create professional-looking spreadsheets.

How can I center text in Excel cells programmatically?

To center text in Excel cells using IronXL, set the Style.HorizontalAlignment property to IronXL.Styles.HorizontalAlignment.Center. You can also control vertical alignment using the VerticalAlignment property for complete text positioning control.

Can I apply borders and alignment to multiple cells at once?

Yes, IronXL allows you to apply borders and alignment to entire ranges, columns, or rows. Simply select your desired range and apply the Style properties to format multiple cells simultaneously, making bulk formatting operations efficient.

How do I set border colors for Excel cells in C#?

IronXL enables you to set border colors using either predefined color types or custom Hex color codes. Access the border color property through the Style object to customize the appearance of your cell borders.

What's the minimal code needed to add borders and center text?

With IronXL, you can add borders and center text in just two lines: workSheet["B2"].Style.LeftBorder.Type = IronXL.Styles.BorderType.MediumDashed; and workSheet["B2"].Style.HorizontalAlignment = IronXL.Styles.HorizontalAlignment.Center; This provides quick formatting without Interop overhead.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Ready to Get Started?
Nuget Downloads 1,780,288 | Version: 2025.12 just released