C#-Write Hyperlink in Excel

It is common that we insert hyperlinks in one document if we need to reference contents of one websites. In MS Excel, sometimes, hyperlink will be data to show some necessary information. For example, if we want to check page views for web pages of one sites, we need to show address of each page firstly. Therefore, we need to write hyperlinks which link to webpages in cells. Also, E-mail can be shown by hyperlink in Excel.

Now, in this post, I will show my way to write hyperlinks in Excel worksheet with C#.

In this method, I use component, Spire.XLS. So firstly, I add its dll file as reference. Then, I add another reference, system.drawing for setting format.

When writing the hyperlink, I use sheet.Hyperlinks.Add() which can create a hyperlink. Then, use hylink.Type and hylink.Address to set its type and address.

Use the following code:

using System;
using System.Drawing;
using Spire.Xls;

namespace InsertHyperlink
{
    class Hyperlink
    {
        static void Main(string[] args)
        {
            //Initialize Worksheet
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            //Set Text Format
            sheet.Range["B2"].Text = "Google";
            sheet.Range["B2"].Style.Font.FontName = "Century Gothic";
            sheet.Range["B2"].Style.Font.IsBold = true;
            sheet.Range["B2"].Style.Font.Size = 12;
            sheet.Range["B2"].Style.Font.Color = Color.DarkOrange;

            //Write Hyperlink and Set Format
            HyperLink hylink1 = sheet.HyperLinks.Add(sheet.Range["B3"]);
            hylink1.Type = HyperLinkType.Url;
            hylink1.Address = @"http://www.google.com";

            sheet.Range["B3"].Style.Font.FontName = "Century Gothic";
            sheet.Range["B3"].Style.Font.Color = Color.Red;
            sheet.Range["B3"].Style.Font.Size = 12;

            //Set Text Format
            sheet.Range["B5"].Text = "Contact Google";
            sheet.Range["B5"].Style.Font.FontName = "Century Gothic";
            sheet.Range["B5"].Style.Font.IsBold = true;
            sheet.Range["B5"].Style.Font.Size = 12;
            sheet.Range["B5"].Style.Font.Color = Color.RoyalBlue;

            //Write Hyperlink and Set Format
            HyperLink hylink2 = sheet.HyperLinks.Add(sheet.Range["B6"]);
            hylink2.Type = HyperLinkType.Url;
            hylink2.Address = "http://www.google.com/intl/en/contact/";

            sheet.Range["B6"].Style.Font.FontName = "Century Gothic";
            sheet.Range["B6"].Style.Font.Color = Color.LimeGreen;
            sheet.Range["B6"].Style.Font.Size = 12;

            //Save File
            workbook.SaveToFile("Hyperlink.xls");

            //Launch File
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

After running, we can get result as following.

—————————————————————————————————————————————-

Click Here to learn more about Spire.XLS.

Click Here to download Spire.XLS.

Advertisements

One thought on “C#-Write Hyperlink in Excel

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