IronXL How-Tos Border & Alignment How to Set Cell Border and Alignment Chaknith Bin Updated:July 28, 2025 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. With IronXL, you can enhance data visualization, improve readability, and create professional-looking spreadsheets by customizing border styles, thickness, colors, and aligning text to better present your data. How to Set Cell Border and Alignment Download the C# library to set background pattern and color Open an existing or create a new Excel spreadsheet Set the Type property of the border to one of the available border types in BorderType Align horizontally or vertically using the HorizontalAlignment and VerticalAlignment properties Set the border color from an available type or a Hex Color code Get started with IronXL Start using IronXL in your project today with a free trial. First Step: Start for Free 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 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 noteSetting 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 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 Border Patterns Alignment Types Discover the full range of alignment options offered by IronXL in the illustration below: 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 How do you set cell borders in Excel using C#? To set cell borders in Excel using C#, you can use IronXL's properties like TopBorder, RightBorder, BottomBorder, and LeftBorder. Customize each side with different BorderType and color using the Color class or Hex codes. What methods are available for aligning text in Excel cells using C#? IronXL allows you to set text alignment in Excel cells by using the HorizontalAlignment and VerticalAlignment properties. You can choose from options like Left, Center, Right, and Top, Center, Bottom for precise data presentation. How can I apply different border styles to Excel cells in C#? IronXL provides a range of border styles, such as Thin, Double, Dashed, Dotted, and Solid. You can apply these styles to each side of a cell to enhance your spreadsheet's appearance. Can you explain how to customize border colors in Excel using a C# library? Yes, with IronXL, you can easily customize border colors by setting the Color property using Hex color codes or predefined color types, giving you full control over your spreadsheet's aesthetics. What is the process for saving an Excel workbook after customizing it in C#? After customizing cell borders and alignments using IronXL, you can save the workbook with the SaveAs method, specifying the desired file name and format, ensuring your changes are preserved. How does the 'Fill' alignment work in C# Excel libraries? In IronXL, the 'Fill' alignment will stretch the cell's content to fill the entire cell width. If adjacent cells also have 'Fill' alignment, the content extends into those cells as well, creating a seamless look. What are the advantages of using IronXL for Excel manipulation in C#? Using IronXL for Excel manipulation in C# offers many advantages, including enhanced data visualization, better readability, and the ability to create professional-looking spreadsheets without relying on Excel Interop. How can I troubleshoot common issues when setting borders in Excel using C#? Common issues can include incorrect border application or color mismatches. Ensure that the BorderType and Color properties are correctly set. Double-check your Hex codes and property assignments when using IronXL. What is the 'Justify' vertical alignment and how is it used in C#? The 'Justify' vertical alignment in IronXL spreads text evenly across a cell's height, with top and bottom margins, similar to horizontal justification, providing a neat and balanced appearance. Chaknith Bin Chat with engineering team now 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? Free NuGet Download Total downloads: 1,558,208 View Licenses