NativeExcel2 for .NET Developer's Reference

Font formatting

Example

The following example demonstrates how to change font formatting.

Output file: console-font.xls

[C#]
using System;
using System.Drawing;
using NativeExcel;

namespace Console_Font
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-font.xls";
            CreateWorkbook(FileName);
            OpenWorkbookWithExcel(FileName);
        }

        static void CreateWorkbook(string FileName) {
            //Create a new workbook
            IWorkbook book = NativeExcel.Factory.CreateWorkbook();

            //Add worksheet
            IWorksheet sheet = book.Worksheets.Add();
            IRange range = sheet.Range["B2:F10"];

            //Set value to "Text" for each cell in the range
            range.Value = "Text";

            //Set the font name and size
            range.Font.Name = "Times New Roman";
            range.Font.Size = 15;

            //Set the font style to bold for the first row of the range
            range.Rows[1].Font.Bold = true;

            //Set the font style to italic for the first column of the range
            range.Columns[1].Font.Italic = true;

            //Set the font color for the second row of the range 
            range.Rows[2].Font.Color = Color.Red;

            //Set the font color, size and underline for the third row
            //of the range
            range.Rows[3].Font.Color = Color.Green;
            range.Rows[3].Font.Size = 8;
            range.Rows[3].Font.Underline = XlUnderlineStyle.xlUnderlineStyleSingle;

            //Set the font name for the second column of the range
            range.Columns[2].Font.Name = "Arial"; 

            //Set the font size for the range E8:F10
            sheet.Range["E8:F10"].Font.Size = 10;
            //Set the font style to bold for the range E8:F10
            sheet.Range["E8:F10"].Font.Bold = true;
            //Set the borders style for the range E8:F10  
            sheet.Range["E8:F10"].Borders.LineStyle = XlLineStyle.xlDot;

            //Save workbook
            book.SaveAs(FileName);
        }

        static void OpenWorkbookWithExcel(string FileName){
            try {
                System.Diagnostics.Process.Start(FileName);
            } catch {
                Console.WriteLine(FileName + " created in application folder");
            }
        }
    }
}
[Visual Basic]
imports System
imports NativeExcel

Module Console_Font

    Sub Main()
        Dim FileName As String = "console-font.xls"
        CreateWorkbook(FileName)
        OpenWorkbookWithExcel(FileName)
    End Sub

    Sub CreateWorkbook(FileName As String)

        'Create a new workbook
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()

        'Add worksheet
        Dim sheet As IWorksheet = book.Worksheets.Add()
        Dim range As IRange     = sheet.Range("B2:F10")

        'Set value to "Text" for each cell in the range
        range.Value = "Text"

        'Set the font name and size
        range.Font.Name = "Times New Roman"
        range.Font.Size = 15

        'Set the font style to bold for the first row of the range  
        range.Rows(1).Font.Bold = True

        'Set the font style to italic for the first column of the range
        range.Columns(1).Font.Italic = True

        'Set the font color for the second row of the range 
        range.Rows(2).Font.Color = System.Drawing.Color.Red

        'Set the font color, size and underline for the third row
        'of the range
        range.Rows(3).Font.Color = System.Drawing.Color.Green
        range.Rows(3).Font.Size = 8
        range.Rows(3).Font.Underline = XlUnderlineStyle.xlUnderlineStyleSingle
            
        'Set the font name for the second column of the range
        range.Columns(2).Font.Name = "Arial"

        'Set the font size for the range E8:F10
        sheet.Range("E8:F10").Font.Size = 10
        'Set the font style to bold for the range E8:F10
        sheet.Range("E8:F10").Font.Bold = true
        'Set the borders style for the range E8:F10
        sheet.Range("E8:F10").Borders.LineStyle = XlLineStyle.xlDot

        'Save workbook
        book.SaveAs(FileName)

    End Sub

    Sub OpenWorkbookWithExcel(FileName As String)
        Try
           System.Diagnostics.Process.Start(FileName)
        Catch
           Console.WriteLine(FileName + " created in application folder")
        End Try  
    End Sub

End Module