How to Set Excel Font Style by Using C#

Microsoft Excel provides lots of font styles for setting text font in cells. User can choose the most favorite font style for setting text or numbers. Because of font style, the appearance of Excel file will be more wonderful. Also, sometimes, users may set different font styles for different cells. For example, the font style of header row is Impact to emphasize, while data body font style is Times New Roman. 

Besides, font size and color are contained in font style settings. Bigger size and bright color can tell readers that the data information is special or very important. 

In this post, I will introduce a method about font format settings, including font type, size, color etc. This method is based on a C# .NET Excel component, Spire.XLS for .NET. Therefore, I have add its dll file as reference in project at the beginning. 

The following code shows details about how to set Excel font style. Firstly, create a new workbook and initialize worksheet. Secondly, add text in specified cells to tell readers which style I will set and set style for text. Finally, save and launch this workbook.

Coding:

            //Create Workbook and Worksheet
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Set Font Type as Calibri
            sheet.Range[“B1”].Text = “Calibri”;
            sheet.Range[“B1”].Style.Font.FontName = “Calibri”;

            //Set Font Size as 22
            sheet.Range[“B2”].Text = “Large size”;
            sheet.Range[“B2”].Style.Font.Size = 22;

            //Set Font as Bold
            sheet.Range[“B3”].Text = “Bold”;
            sheet.Range[“B3”].Style.Font.IsBold = true;

            //Set Font as Italic
            sheet.Range[“B4”].Text = “Italic”;
            sheet.Range[“B4”].Style.Font.IsItalic = true;

            //Set Font as Superscript
            sheet.Range[“B5”].Text = “Superscript”;
            sheet.Range[“B5”].Style.Font.IsSuperscript = true;

            //Set Font as Colored
            sheet.Range[“B6”].Text = “Colored”;
            sheet.Range[“B6”].Style.Font.Color = Color.FromArgb(255, 125, 125);

            //Set Font as Underlined
            sheet.Range[“B7”].Text = “Underline”;
            sheet.Range[“B7”].Style.Font.Underline = FontUnderlineType.Single;

            //Save File
            workbook.SaveToFile(“FontStyle.xls”);

            //Launch File
            System.Diagnostics.Process.Start(“FontStyle.xls”);

Result Shown as Following:

Download Spire.XLS for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s