C#/VB.NET: Add or Remove Cell Borders in Excel

Cell borders refer to lines that can be added around a cell or range of cells. They can be used to serve different purposes, such as to separate sections in a worksheet, draw readers' attention to important cells, or make the worksheet look more presentable. This article will introduce how to add or remove cell borders in Excel in C# and VB.NET using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add Cell Borders in Excel in C# and VB.NET

Spire.XLS for .NET allows adding various kinds of borders to cells in Excel, such as left border, right border, top border, bottom border, diagonal borders, inside borders and outside borders.

You can add a specific border or multiple borders to individual cells or ranges of cells. In addition, you can also set different line styles and line colors for the borders. The following are the main steps to apply different kinds of cell borders with different line styles and line colors:

  • Initialize an instance of the Workbook class.
  • Get a specific worksheet by its index through Workbook.Worksheets[int] property.
  • Get a specific cell range by its name through Worksheet.Range[string] property.
  • Get specific borders (such as left, right, top, bottom and diagonal) from the Borders collection of the cell range through CellRange.Borders[BordersLineType] property.
  • Set the line styles of the specific borders through IBorder.LineStyle property.
  • Set the line colors of the specific borders through IBorder.Color property.
  • Get a specific cell range by its name through Worksheet.Range[string] property.
  • Add outside borders and/or inside borders to the cell range using CellRange.BorderAround(LineStyleType, Color) method and/or CellRange.BorderInside(LineStyleType, Color) method. Note that inside borders cannot be applied to a single cell.
  • Get a specific cell range by its name through Worksheet.Range[string] property.
  • Set the line styles and line colors for borders of the cell range through BordersCollection.LineStyle and BordersCollection.Color properties, then set the line style and color for diagonal borders of the cell range.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core;
using System.Drawing;

namespace AddCellBorders
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Set left, right, top, bottom and diagonal up borders for cell B2
            CellRange range = sheet.Range["B2"];
            IBorder leftBorder = range.Borders[BordersLineType.EdgeLeft];
            leftBorder.LineStyle = LineStyleType.MediumDashDotDot;
            leftBorder.Color = Color.Red;
            IBorder rightBorder = range.Borders[BordersLineType.EdgeRight];
            rightBorder.LineStyle = LineStyleType.MediumDashed;
            rightBorder.Color = Color.Red;
            IBorder topBorder = range.Borders[BordersLineType.EdgeTop];
            topBorder.LineStyle = LineStyleType.Medium;
            topBorder.Color = Color.Red;
            IBorder bottomBorder = range.Borders[BordersLineType.EdgeBottom];
            bottomBorder.LineStyle = LineStyleType.Medium;
            bottomBorder.Color = Color.Red;
            IBorder diagonalUpBorder = range.Borders[BordersLineType.DiagonalUp];
            diagonalUpBorder.LineStyle = LineStyleType.Thin;
            diagonalUpBorder.Color = Color.Red;

            //Set diagonal borders for cell C4
            range = sheet.Range["C4"];
            diagonalUpBorder = range.Borders[BordersLineType.DiagonalUp];
            diagonalUpBorder.LineStyle = LineStyleType.Double;
            diagonalUpBorder.Color = Color.Blue;
            IBorder diagonalDownBorder = range.Borders[BordersLineType.DiagonalDown];
            diagonalDownBorder.LineStyle = LineStyleType.Double;
            diagonalDownBorder.Color = Color.Blue;

            //Set outside borders for cell D6
            range = sheet.Range["D6"];
            range.BorderAround(LineStyleType.Double, Color.Green);

            //Set inside borders for cell range E8:F10
            range = sheet.Range["E8:F10"];
            range.BorderInside(LineStyleType.MediumDashed, Color.DarkGray);

            //Set inside and outside borders for cell range F12:G14
            range = sheet.Range["F12:G14"];
            range.BorderInside(LineStyleType.MediumDashed, Color.Pink);
            range.BorderAround(LineStyleType.Medium, Color.Magenta);

            //Set borders for cell range G16:H18
            range = sheet.Range["G16:H18"];
            range.Borders.LineStyle = LineStyleType.Thick;
            range.Borders.Color = Color.Cyan;
            //Set line style and line color of diagonal borders for cell range G16:H18
            diagonalUpBorder = range.Borders[BordersLineType.DiagonalUp];
            diagonalUpBorder.LineStyle = LineStyleType.Dotted;
            diagonalUpBorder.Color = Color.DarkGray;
            diagonalDownBorder = range.Borders[BordersLineType.DiagonalDown];
            diagonalDownBorder.LineStyle = LineStyleType.Dotted;
            diagonalDownBorder.Color = Color.DarkGray;

            //Save the result file
            workbook.SaveToFile("AddBorders.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Add or Remove Cell Borders in Excel

Remove Cell Borders in Excel in C# and VB.NET

You can remove all borders of a cell or range of cells by setting the CellRange.Borders.LineStyle property as LineStyleType.None. The following are the details steps:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index through Workbook.Worksheets[int] property.
  • Get a specific cell range by its name through Worksheet.Range[string] property.
  • Remove the borders of the cell range by setting CellRange.Borders.LineStyle property as LineStyleType.None.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace RemoveCellBorders
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("AddBorders.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Remove borders of cell range G16:H18
            CellRange range = sheet.Range["G16:H18"];
            range.Borders.LineStyle = LineStyleType.None;

            workbook.SaveToFile("RemoveBorders.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Add or Remove Cell Borders in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.