Wednesday, 05 January 2011 08:36

Set Excel Font in C#, VB.NET

Written by  Administrator
Rate this item
(0 votes)

Excel font makes the whole Excel colorful. Whatever you emphasize some special cell data or beautify the whole document, you can start from setting Excel font, such as font style, size, color, bold, underline, italic etc. This section will introduce a solution to set Excel font via a .NET Excel component in C#, VB.NET.

Spire.XLS for .NET, applied both in ASP.NET websites and Windows Forms application, enables users to generate, edit and covert Excel files to other files. Using this Excel component, you can set your Excel font by the class Spire.Xls.CellStyle.Font very quickly. This class enables you to set Excel font style, size, color, bold, underline and italic which are the most commonly used formats, also it can set data alignment, strikethrough, subscript, superscript etc. Please view the Excel font effect in below picture:

Set Excel Cell Font

Here you can download Spire,XLS for .NET. After installing it on system and adding Spire.Xls dll, you can begin to set Excel font by below code.

Whole Code:

using Spire.Xls;

namespace Excel_font
    class Program
        static void Main(string[] args)
            //create a new workbook and load Excel data
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\Excel Font.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            //Set excel font style
            sheet.Range["A3:E3"].Style.Font.FontName = "Comic Sans MS";
            sheet.Range["A4:E14"].Style.Font.FontName = "Corbel";
            //set excel font size
            sheet.Range["A2:E2"].Style.Font.Size = 50;
            sheet.Range["A3:E3"].Style.Font.Size = 25;
            sheet.Range["A4:E14"].Style.Font.Size = 12;
            //set excel cell data to be bold
            sheet.Range["A3:E3"].Style.Font.IsBold = true;
            //set excel cell data to be underline
            sheet.Range["A3:E3"].Style.Font.Underline = FontUnderlineType.Single;
            //set excel cell data color
            sheet.Range["A3:E3"].Style.Font.Color = Color.DarkOliveGreen;
            sheet.Range["A4:E14"].Style.Font.Color = Color.Firebrick;
            //set excel cell data to be italic
            sheet.Range["A4:E14"].Style.Font.IsItalic = true;

            //save and launch the file
Imports Spire.Xls

Namespace Excel_font
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'create a new workbook and load Excel data
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("..\Excel Font.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Set excel font style
            sheet.Range("A3:E3").Style.Font.FontName = "Comic Sans MS"
            sheet.Range("A4:E14").Style.Font.FontName = "Corbel"
            'set excel font size
            sheet.Range("A2:E2").Style.Font.Size = 50
            sheet.Range("A3:E3").Style.Font.Size = 25
            sheet.Range("A4:E14").Style.Font.Size = 12
            'set excel cell data to be bold
            sheet.Range("A3:E3").Style.Font.IsBold = true
            'set excel cell data to be underline
            sheet.Range("A3:E3").Style.Font.Underline = FontUnderlineType.Single
            'set excel cell data color
            sheet.Range("A3:E3").Style.Font.Color = Color.DarkOliveGreen
            sheet.Range("A4:E14").Style.Font.Color = Color.Firebrick
            'set excel cell data to be italic
            sheet.Range("A4:E14").Style.Font.IsItalic = true
            'save and launch the file
        End Sub
    End Class
End Namespace

Additional Info

  • tutorial_title: Set Excel Font
Last modified on Tuesday, 29 July 2014 06:06