NativeExcel2 for .NET Developer's Reference

Font features

Example

This example demonstrates the major font formatting features.

Output file: console-fontfeatures.xls

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

namespace Console_FontFeatures
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-fontfeatures.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 cells = sheet.Cells;

            int row = 2;

            cells[row, 1].Value = "Font.Name = \"Times New Roman\";";
            cells[row, 1].Font.Name = "Times New Roman";

            row++;
            cells[row, 1].Value = "Font.Size = 16;";
            cells[row, 1].Font.Size = 16;

            row++;
            cells[row, 1].Value = "Font.Italic = true;";
            cells[row, 1].Font.Italic = true;

            row++;
            cells[row, 1].Value = "Font.Bold = true;";
            cells[row, 1].Font.Bold = true;

            row++;
            cells[row, 1].Value = "Font.Subscript = true;";
            cells[row, 1].Font.Subscript = true;

            row++;
            cells[row, 1].Value = "Font.Superscript = true;";
            cells[row, 1].Font.Superscript = true;

            row++;
            cells[row, 1].Value = "Font.Underline = XlUnderlineStyle.xlUnderlineStyleDouble;";
            cells[row, 1].Font.Underline = XlUnderlineStyle.xlUnderlineStyleDouble;

            row++;
            cells[row, 1].Value = "Font.Strikethrough = true;";
            cells[row, 1].Font.Strikethrough = true;

            row++;
            cells[row, 1].Value = "Font.Color = System.Drawing.Color.Blue;";
            cells[row, 1].Font.Color = System.Drawing.Color.Blue;

            row++;
            cells[row, 1].Value = "Font.ColorIndex = 3; //Red";
            cells[row, 1].Font.ColorIndex = 3;
            
            //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 System.Drawing
imports NativeExcel

Module Console_FontFeatures

    Sub Main()
        Dim FileName As String = "console-fontfeatures.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 cells As IRange     = sheet.Cells

        Dim row As Integer = 2

        cells(row, 1).Value = "Font.Name = ""Times New Roman"""
        cells(row, 1).Font.Name = "Times New Roman"

        row = row + 1
        cells(row, 1).Value = "Font.Size = 16"
        cells(row, 1).Font.Size = 16

        row = row + 1
        cells(row, 1).Value = "Font.Italic = True"
        cells(row, 1).Font.Italic = True

        row = row + 1
        cells(row, 1).Value = "Font.Bold = True"
        cells(row, 1).Font.Bold = True

        row = row + 1
        cells(row, 1).Value = "Font.Subscript = True"
        cells(row, 1).Font.Subscript = True

        row = row + 1
        cells(row, 1).Value = "Font.Superscript = True"
        cells(row, 1).Font.Superscript = True

        row = row + 1
        cells(row, 1).Value = "Font.Underline = XlUnderlineStyle.xlUnderlineStyleDouble"
        cells(row, 1).Font.Underline = XlUnderlineStyle.xlUnderlineStyleDouble

        row = row + 1
        cells(row, 1).Value = "Font.Strikethrough = True"
        cells(row, 1).Font.Strikethrough = True

        row = row + 1
        cells(row, 1).Value = "Font.Color = System.Drawing.Color.Blue"
        cells(row, 1).Font.Color = System.Drawing.Color.Blue

        row = row + 1
        cells(row, 1).Value = "Font.ColorIndex = 3"
        cells(row, 1).Font.ColorIndex = 3
        
        '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