Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
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;

// This code demonstrates how to create an Excel workbook, modify a specific cell by adjusting its value,
// borders, and alignment, and then save the workbook to a file.

// Create a new Excel workbook
WorkBook workBook = WorkBook.Create();

// Access the default worksheet within the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Assign a value to cell B2
workSheet["B2"].Value = "B2";

// Set the left and right borders for cell B2 to be medium dashed
workSheet["B2"].Style.LeftBorder.Type = BorderType.MediumDashed;
workSheet["B2"].Style.RightBorder.Type = BorderType.MediumDashed;

// Center align the text within cell B2
workSheet["B2"].Style.Alignment.Horizontal = HorizontalAlignment.Center;

// Save the workbook to a file with the specified name
workBook.SaveAs("setBorderAndAlignment.xlsx");
Imports IronXL

Imports IronXL.Styles



' This code demonstrates how to create an Excel workbook, modify a specific cell by adjusting its value,

' borders, and alignment, and then save the workbook to a file.



' Create a new Excel workbook

Private workBook As WorkBook = WorkBook.Create()



' Access the default worksheet within the workbook

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Assign a value to cell B2

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



' Set the left and right borders for cell B2 to be medium dashed

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

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



' Center align the text within cell B2

Private workSheet("B2").Style.Alignment.Horizontal = HorizontalAlignment.Center



' Save the workbook to a file with the specified name

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.

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.

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

// This script creates a new Excel workbook and sets specific styles for a cell in a worksheet.

// Create a new workbook
WorkBook workBook = WorkBook.Create();

// Access the default worksheet in the newly created workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the left and right border types of the cell B2 to Thick
workSheet["B2"].Style.LeftBorder.Type = BorderType.Thick;
workSheet["B2"].Style.RightBorder.Type = BorderType.Thick;

// Set the left border color to Aquamarine using the predefined Color enumeration
workSheet["B2"].Style.LeftBorder.SetColor(Color.Aquamarine);

// Set the right border color using a hex color code
workSheet["B2"].Style.RightBorder.SetColor("#FF7F50");

// Save the workbook to a file named "setBorderColor.xlsx"
workBook.SaveAs("setBorderColor.xlsx");
Imports IronXL

Imports IronXL.Styles

Imports IronSoftware.Drawing



' This script creates a new Excel workbook and sets specific styles for a cell in a worksheet.



' Create a new workbook

Private workBook As WorkBook = WorkBook.Create()



' Access the default worksheet in the newly created workbook

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Set the left and right border types of the cell B2 to Thick

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

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



' Set the left border color to Aquamarine using the predefined Color enumeration

workSheet("B2").Style.LeftBorder.SetColor(Color.Aquamarine)



' Set the right border color using a hex color code

workSheet("B2").Style.RightBorder.SetColor("#FF7F50")



' Save the workbook to a file named "setBorderColor.xlsx"

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;

// This code snippet demonstrates creating an Excel workbook with some basic styling using IronXL library.

// Create a new workbook
WorkBook workBook = WorkBook.Create();

// Access the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the string value for cell B2
workSheet["B2"].StringValue = "Top";

// Set the string value for cell B4
workSheet["B4"].StringValue = "Forward";

// Set a thick top border for cell B2
workSheet["B2"].Style.TopBorder.Type = BorderType.Thick;

// Set a thick diagonal border for cell B4
workSheet["B4"].Style.DiagonalBorder.Type = BorderType.Thick;

// Set the diagonal border direction to 'Forward' for cell B4
workSheet["B4"].Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward;

// Save the workbook to a file named "borderLines.xlsx"
// The SaveAs method generates the file at the specified path
workBook.SaveAs("borderLines.xlsx");
Imports IronXL

Imports IronXL.Styles



' This code snippet demonstrates creating an Excel workbook with some basic styling using IronXL library.



' Create a new workbook

Private workBook As WorkBook = WorkBook.Create()



' Access the default worksheet in the workbook

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Set the string value for cell B2

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



' Set the string value for cell B4

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



' Set a thick top border for cell B2

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



' Set a thick diagonal border for cell B4

Private workSheet("B4").Style.DiagonalBorder.Type = BorderType.Thick



' Set the diagonal border direction to 'Forward' for cell B4

Private workSheet("B4").Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward



' Save the workbook to a file named "borderLines.xlsx"

' The SaveAs method generates the file at the specified path

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.

Frequently Asked Questions

What is IronXL?

IronXL is a C# library that allows developers to manipulate Excel files, including setting borders and alignments, without using Interop.

How do you set a cell border using IronXL?

You can set a cell border by using the Border properties such as TopBorder, RightBorder, BottomBorder, and LeftBorder, and specifying the BorderType and color.

How can you align text in an Excel cell using IronXL?

Text alignment can be set by modifying the HorizontalAlignment and VerticalAlignment properties using the IronXL.Styles.HorizontalAlignment and IronXL.Styles.VerticalAlignment enums.

What are the available border types in IronXL?

IronXL provides various border types such as Thin, Double, Dashed, Dotted, and Solid, which can be applied to each side of a cell.

Can you customize border colors in IronXL?

Yes, you can customize border colors by setting the Color property using a Hex color code or predefined color types.

What does the 'Fill' horizontal alignment do in IronXL?

The 'Fill' alignment fills the cell's value across its width. If adjacent cells also have the 'Fill' alignment, the content will continue to fill them as well.

How do you apply a border pattern using IronXL?

Border patterns can be applied by choosing different BorderType options for each side of a cell, such as Dashed or Dotted.

What is the 'Justify' vertical alignment in IronXL?

The 'Justify' vertical alignment distributes text evenly across the cell's height, with flush top and bottom margins, similar to horizontal justification.

How can you save a workbook after setting borders and alignments?

After setting the desired styles, you can save the workbook using the SaveAs method, specifying the file name and format.

What are the benefits of using IronXL for Excel manipulation?

IronXL allows for enhanced data visualization, improved readability, and the creation of professional-looking spreadsheets by offering customization of borders and text alignments without needing Excel Interop.

Chaknith related to VerticalAlignment Enumeration
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.