Add Excel Borders in C#, VB.NET

In Excel, you can add borders to individual cells to emphasize data, mark summarized values, or separate data in cell ranges. Borders can also be formatted with different line styles and colors. This article presents how we can apply border styles to Excel cells using Spire.XLS.

Step 1: Create a Workbook instance and get the first worksheet.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Get the cell range where you want to apply border style.

CellRange range = sheet.Range["B2:D4"];

Step 3: Call BorderInside() and BorderAround() methods to apply styles to borders inside and around the range. To add a border to the top, bottom, left and right, use the Borders[BordersLineType.EdgeTop].LineStyle property, Borders[BordersLineType.EdgeBottom].LineStyle property and alike.

range.BorderInside(LineStyleType.Thin, Color.Blue);
range.BorderAround(LineStyleType.Medium, Color.Blue);

Step 4: Save to file.

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);


Add Excel Borders in C#, VB.NET

Full Code:

using Spire.Xls;
using System.Drawing;
namespace AddExcelBorder

    class Program

        static void Main(string[] args)
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];

            //apply border style to the range B2:D4
            CellRange range = sheet.Range["B2:D4"];
            range.BorderInside(LineStyleType.Thin, Color.Blue);
            range.BorderAround(LineStyleType.Medium, Color.Blue);

            //apply border style to the range D7:F9
            range = sheet.Range["D7:F9"];
            range.Borders.LineStyle = LineStyleType.Double;
            range.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            range.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;

            //apply border style to the range F12:H14
            range = sheet.Range["F12:H14"];
            range.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.Thin;
            range.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thick;
            range.Borders.Color = Color.Red;

            wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);

Imports Spire.Xls
Imports System.Drawing
Namespace AddExcelBorder

	Class Program

		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			Dim sheet As Worksheet = wb.Worksheets(0)

			'apply border style to the range B2:D4
			Dim range As CellRange = sheet.Range("B2:D4")
			range.BorderInside(LineStyleType.Thin, Color.Blue)
			range.BorderAround(LineStyleType.Medium, Color.Blue)

			'apply border style to the range D7:F9
			range = sheet.Range("D7:F9")
			range.Borders.LineStyle = LineStyleType.[Double]
			range.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			range.Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None

			'apply border style to the range F12:H14
			range = sheet.Range("F12:H14")
			range.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.Thin
			range.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thick
			range.Borders.Color = Color.Red

			wb.SaveToFile("result.xlsx", ExcelVersion.Version2013)

		End Sub
	End Class
End Namespace