IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: July 21, 2026

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.

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. 2Copy and run this code snippet.

    workSheet["B2"].Style.LeftBorder.Type = IronXL.Styles.BorderType.MediumDashed;
    workSheet["B2"].Style.HorizontalAlignment = IronXL.Styles.HorizontalAlignment.Center;
    C#
  3. 3Deploy 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 [BorderType] enum. Explore all available border types to find the perfect match.

IronXL.Styles.BorderType HorizontalAlignment VerticalAlignment IronXL.Styles.HorizontalAlignment IronXL.Styles.VerticalAlignment

For precise text alignment, adjust the [HorizontalAlignment] and [VerticalAlignment] properties in Style to achieve the desired layout. Use the [HorizontalAlignment] and [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:

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");
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 note: Setting 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.

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");
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.

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");

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);

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.

Color Color

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 apply a MediumDashed border to an Excel cell using IronXL?

To apply a MediumDashed border to an Excel cell using IronXL, you can set the border type for the desired border (e.g., LeftBorder, RightBorder) using the IronXL.Styles.BorderType.MediumDashed property on the cell's Style.

What is the easiest way to set text alignment in Excel using C# and IronXL?

IronXL allows you to set text alignment easily by using the HorizontalAlignment and VerticalAlignment properties on a cell's Style, allowing you to align text horizontally as Center, Left, or Right and vertically as Top, Middle, or Bottom, among other alignments.

Can I customize border colors for Excel cells with IronXL?

Yes, you can customize border colors in IronXL by using the SetColor method with the IronXL.Styles.Color class or a hex color code to specify your desired border color.

How can I apply center alignment to text in an Excel cell using IronXL?

To center align text in an Excel cell using IronXL, set the HorizontalAlignment property of the cell's Style to IronXL.Styles.HorizontalAlignment.Center.

Is it possible to apply borders to multiple sides of an Excel cell with IronXL?

Yes, IronXL allows you to apply borders to multiple sides of an Excel cell by individually setting border types such as TopBorder, RightBorder, BottomBorder, and LeftBorder on the cell's Style.

What are the available enums for setting text alignment in IronXL?

IronXL provides HorizontalAlignment and VerticalAlignment enums to set text alignment, including options like Left, Center, Right for horizontal, and Top, Middle, Bottom for vertical alignment.

How do I ensure seamless visuals while applying borders in IronXL?

In IronXL, apply consistent border types and colors using the same BorderType and SetColor methods across cells to ensure seamless visuals, and consider combining them with other formatting options like conditional formatting for dynamic styling.

Can IronXL handle both horizontal and vertical text alignment in Excel sheets?

Yes, IronXL can manage both horizontal and vertical text alignment through the HorizontalAlignment and VerticalAlignment properties, allowing comprehensive control over how text appears in cells.

What function can I use to apply a uniform border across multiple cells using IronXL?

You can create a custom function like ApplyUniformBorder, which takes parameters for the worksheet, cell range, border type, and color, to apply consistent borders across multiple cells using IronXL's BorderType and Coloring features.

How does IronXL facilitate professional spreadsheet formatting?

IronXL supports detailed formatting features such as precise control over cell borders and text alignment, including advanced border types and alignment options, which enable you to create professional-looking spreadsheets and reports effortlessly.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 2,237,574Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronXL"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronXL to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronXL.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required