Wednesday, 05 January 2011 08:37

Add Excel Borders in C#, VB.NET

Written by  Administrator
Rate this item
(0 votes)

With Excel borders, the layout will be more wonderful and data information in cells will be more obvious. According to own requirements, users can set several kinds of formatting for borders, such as border styles (thin or thick, signal or double etc.) and border colors.

Spire.XLS for .NET, an easy-to-use .NET component which is used to operate MS Excel file without automation, enables users to add Excel borders and set border format by using C#, VB.NET. And this guide will focus on how to realize this function via Spire.XLS for .NET. The following screenshot is cells in Excel which has been formatted with borders.

Add Excel Cell Borders

With Spire.XLS for .NET, users just need to set LineStyle and Color properties of Borders after choosing data range to add and format borders for the specified cells. Now Download and install Spire.XLS for .NET and use the following code to add Excel borders.

[C#]
using Spire.Xls;
using System.Drawing;

namespace ExcelBorders
{
    class CellBorder
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"E:\Work\Documents\ExcelFiles\Staff Contact Info.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Range["A1:E15"].Borders.LineStyle = LineStyleType.Double;
            sheet.Range["A1:E15"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["A1:E15"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["A1:E15"].Borders.Color = Color.DeepSkyBlue;

            workbook.SaveToFile("Borders.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("Borders.xlsx");
        }
    }
}

[VB.NET]
Imports Spire.Xls
Imports System.Drawing

Namespace ExcelBorders
    Friend Class CellBorder
        Shared Sub Main(ByVal args() As String)
            Dim workbook As New Workbook()
            workbook.LoadFromFile("E:\Work\Documents\ExcelFiles\Staff Contact Info.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)

            sheet.Range("A1:E15").Borders.LineStyle = LineStyleType.Double
            sheet.Range("A1:E15").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
            sheet.Range("A1:E15").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
            sheet.Range("A1:E15").Borders.Color = Color.DeepSkyBlue

            workbook.SaveToFile("Borders.xlsx", ExcelVersion.Version2010)
            System.Diagnostics.Process.Start("Borders.xlsx")
        End Sub
    End Class
End Namespace

Spire.XLS allows user to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. As a professional WPF/.NET/Silverlight Excel component, it owns the ability of inserting content into Excel document, formatting cells and converting Excel documents to popular office file formats. Spire.XLS for WPF supports Excel 97-2003, Excel 2007 and Excel 2010.

Additional Info

  • tutorial_title: Add Excel Borders
Last modified on Friday, 11 July 2014 02:42