NativeExcel for .NET Developer's Reference

Hyperlinks

Example

This example demonstrates how to add hyperlinks into worksheet

Output file: console-hyperlinks.xls

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

namespace Console_Hyperlinks
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-hyperlinks.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();
            IWorksheet sheet2 = book.Worksheets.Add();
            sheet2.Name = "Sheet2"; 

            sheet.Cells.Columns[1,2].ColumnWidth = 20;

            //Hyperlink to http://www.nika-soft.com/
            sheet.Cells["A2"].Value = "Hyperlink to http://www.nika-soft.com/";
            sheet.Hyperlinks.Add("A3", "http://www.nika-soft.com/");
            
            //Hyperlink to range within the same worksheet
            sheet.Cells["A4"].Value = "Hyperlink to range within the same worksheet";
            sheet.Hyperlinks.Add(sheet.Cells[5,1], "", "F1:H5");
            sheet.Cells["F1:H5"].Interior.Color = Color.Green;

            //Hyperlink to range on worksheet two
            sheet.Cells["A6"].Value = "Hyperlink to range on worksheet two";
            sheet.Hyperlinks.Add("A7", "", "Sheet2!B5:D8");
            sheet2.Cells["B5:D8"].Interior.Color = Color.Green;

            //Mailto hyperlink
            sheet.Cells["A8"].Value = "Mailto hyperlink";
            sheet.Hyperlinks.Add("A9", "mailto:myname@mailbox.com");

            //Mailto hyperlink with screen tip and display text
            sheet.Cells["A10"].Value = "Mailto hyperlink with screen tip and display text";
            sheet.Hyperlinks.Add("A11", "mailto:myname@mailbox.com",
                                 "", "Click here to mail",
                                 "myname@mailbox.com");

            //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_Hyperlinks

    Sub Main()
        Dim FileName As String = "console-hyperlinks.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 sheet2 As IWorksheet = book.Worksheets.Add()
        sheet2.Name = "Sheet2"

        sheet.Cells.Columns(1,2).ColumnWidth = 20

        'Hyperlink to http://www.nika-soft.com/
        sheet.Cells("A2").Value = "Hyperlink to http://www.nika-soft.com/"
        sheet.Hyperlinks.Add("A3", "http://www.nika-soft.com/")
        
        'Hyperlink to range within the same worksheet
        sheet.Cells("A4").Value = "Hyperlink to range within the same worksheet"
        sheet.Hyperlinks.Add(sheet.Cells(5,1), "", "F1:H5")
        sheet.Cells("F1:H5").Interior.Color = Color.Green

        'Hyperlink to range on worksheet two
        sheet.Cells("A6").Value = "Hyperlink to range on worksheet two"
        sheet.Hyperlinks.Add("A7", "", "Sheet2!B5:D8")
        sheet2.Cells("B5:D8").Interior.Color = Color.Green

        'Mailto hyperlink
        sheet.Cells("A8").Value = "Mailto hyperlink"
        sheet.Hyperlinks.Add("A9", "mailto:myname@mailbox.com")

        'Mailto hyperlink with screen tip and display text
        sheet.Cells("A10").Value = "Mailto hyperlink with screen tip and display text"
        sheet.Hyperlinks.Add("A11", "mailto:myname@mailbox.com", _
                             "", "Click here to mail", _
                             "myname@mailbox.com")

        '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