Design Borders in Excel in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to design borders in Excel workbook via Spire.XLS.

using Spire.Xls;
using System.Drawing;

namespace Borders
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a workbook
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["B2"].Text = "Home Page";
            
            //set the border of Range["B2"]
            sheet.Range["B2"].Borders.LineStyle = LineStyleType.Thick;
            sheet.Range["B2"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["B2"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["B2"].Borders.Color = Color.Red;
            sheet.AutoFitColumn(2);

            //set the border of Range["C4"]
            sheet.Range["C4"].Borders.LineStyle = LineStyleType.Double;
            sheet.Range["C4"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["C4"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["C4"].Borders.Color = Color.Green;

            //set the border of Range["D6"]
            sheet.Range["D6"].Borders.LineStyle = LineStyleType.Medium;
            sheet.Range["D6"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["D6"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["D6"].Borders.Color = Color.HotPink;
           
            //set the border of Range["E8"]
            sheet.Range["E8"].Borders.LineStyle = LineStyleType.Dashed;
            sheet.Range["E8"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["E8"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["E8"].Borders.Color = Color.Yellow;
           
            //set the border of Range["F10"]
            sheet.Range["F10"].Borders.LineStyle = LineStyleType.Hair;
            sheet.Range["F10"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["F10"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["F10"].Borders.Color = Color.SeaGreen;
           
            //set the border of Range["D6"]
            sheet.Range["G12"].Borders.LineStyle = LineStyleType.SlantedDashDot;
            sheet.Range["G12"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["G12"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["G12"].Borders.Color = Color.Blue;
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports Spire.Xls
Imports System.Drawing

Namespace Borders
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			'create a workbook
			Dim workbook As New Workbook()
			Dim sheet As Worksheet = workbook.Worksheets(0)
			sheet.Range("B2").Text = "Home Page"

			'set the border of Range["B2"]
			sheet.Range("B2").Borders.LineStyle = LineStyleType.Thick
			sheet.Range("B2").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("B2").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("B2").Borders.Color = Color.Red
			sheet.AutoFitColumn(2)

			'set the border of Range["C4"]
			sheet.Range("C4").Borders.LineStyle = LineStyleType.Double
			sheet.Range("C4").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("C4").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("C4").Borders.Color = Color.Green

			'set the border of Range["D6"]
			sheet.Range("D6").Borders.LineStyle = LineStyleType.Medium
			sheet.Range("D6").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("D6").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("D6").Borders.Color = Color.HotPink

			'set the border of Range["E8"]
			sheet.Range("E8").Borders.LineStyle = LineStyleType.Dashed
			sheet.Range("E8").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("E8").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("E8").Borders.Color = Color.Yellow

			'set the border of Range["F10"]
			sheet.Range("F10").Borders.LineStyle = LineStyleType.Hair
			sheet.Range("F10").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("F10").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("F10").Borders.Color = Color.SeaGreen

			'set the border of Range["D6"]
			sheet.Range("G12").Borders.LineStyle = LineStyleType.SlantedDashDot
			sheet.Range("G12").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("G12").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("G12").Borders.Color = Color.Blue
			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class