Style Excel Cell Borders & Fonts (Code Example)
Published: 2022-02-11 08:01:56

---
canonical_url: https://ironsoftware.com/csharp/excel/examples/excel-style-cells-borders-fonts
---

IronXL allows C# developers to style any Excel `Cell` and `Range` in C#. For example, it is easy to control: borders, colors, backgrounds, and fonts.

**C#:**
```
using System.Linq;
using IronXL;
using IronXL.Styles;
 
WorkBook workbook = WorkBook.Load("test.xls");
WorkSheet sheet = workbook.WorkSheets.First();
var range = sheet["A1:H10"];
 
var cell = range.First();
 
//This is how we set background color of the cell with an rgb string
cell.Style.SetBackgroundColor("#428D65");
 
//Styling can be applied to the whole range.
 
//This is how we set underline property to the font
//FontUnderlineType is enum that stands for different types of font underlying
range.Style.Font.Underline = FontUnderlineType.SingleAccounting;
 
//This is how we define whether to use horizontal line through the text or not
range.Style.Font.Strikeout = false;
 
//This is how we define whether the font is bold or not
range.Style.Font.Bold = true;
 
//This is how we define whether the font is italic or not
range.Style.Font.Italic = false;
 
//This is how we can get or set script property of a font
//Font script enum stands for available options
range.Style.Font.FontScript = FontScript.Super;
 
//This is how we can set  the type of the border line
//There are also TopBorder,LeftBorder,RightBorder,DiagonalBorder properties
//BorderType enum indicates the line style of a border in a cell
range.Style.BottomBorder.Type = BorderType.MediumDashed;
 
//This is how we can indicate whether the cell should be auto-sized 
range.Style.ShrinkToFit = true;
 
//This is how we can set alignment of the cell
range.Style.VerticalAlignment = VerticalAlignment.Bottom;
 
//This is how we can set border color
range.Style.DiagonalBorder.SetColor("#20C96F");
 
//We need to define border type and border direction as well
range.Style.DiagonalBorder.Type = BorderType.Thick;
 
//DiagonalBorderDirection enum stands for direction of diagonal border inside cell
range.Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward;
 
//Set background color of cells
range.Style.SetBackgroundColor(Color.Aquamarine);
 
//This is how we can set fill pattern of the cell
//FillPattern enum indicates the style of fill pattern
range.Style.FillPattern = FillPattern.Diamonds;
 
//This is how we can set the number of spaces to intend the text
range.Style.Indention = 5;
 
//This is how we can indicate if the text is wrapped
range.Style.WrapText = true;
 
workbook.SaveAs("StylingOptions.xls");
```

**VB:**
```
Imports System.Linq
Imports IronXL
Imports IronXL.Styles
 
Private workbook As WorkBook = WorkBook.Load("test.xls")
Private sheet As WorkSheet = workbook.WorkSheets.First()
Private range = sheet("A1:H10")
 
Private cell = range.First()
 
'This is how we set background color of the cell with an rgb string
cell.Style.SetBackgroundColor("#428D65")
 
'Styling can be applied to the whole range.
 
'This is how we set underline property to the font
'FontUnderlineType is enum that stands for different types of font underlying
range.Style.Font.Underline = FontUnderlineType.SingleAccounting
 
'This is how we define whether to use horizontal line through the text or not
range.Style.Font.Strikeout = False
 
'This is how we define whether the font is bold or not
range.Style.Font.Bold = True
 
'This is how we define whether the font is italic or not
range.Style.Font.Italic = False
 
'This is how we can get or set script property of a font
'Font script enum stands for available options
range.Style.Font.FontScript = FontScript.Super
 
'This is how we can set  the type of the border line
'There are also TopBorder,LeftBorder,RightBorder,DiagonalBorder properties
'BorderType enum indicates the line style of a border in a cell
range.Style.BottomBorder.Type = BorderType.MediumDashed
 
'This is how we can indicate whether the cell should be auto-sized 
range.Style.ShrinkToFit = True
 
'This is how we can set alignment of the cell
range.Style.VerticalAlignment = VerticalAlignment.Bottom
 
'This is how we can set border color
range.Style.DiagonalBorder.SetColor("#20C96F")
 
'We need to define border type and border direction as well
range.Style.DiagonalBorder.Type = BorderType.Thick
 
'DiagonalBorderDirection enum stands for direction of diagonal border inside cell
range.Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward
 
'Set background color of cells
range.Style.SetBackgroundColor(Color.Aquamarine)
 
'This is how we can set fill pattern of the cell
'FillPattern enum indicates the style of fill pattern
range.Style.FillPattern = FillPattern.Diamonds
 
'This is how we can set the number of spaces to intend the text
range.Style.Indention = 5
 
'This is how we can indicate if the text is wrapped
range.Style.WrapText = True
 
workbook.SaveAs("StylingOptions.xls")
```