Insert hyperlink in Excel

  • NPOI
  • Spire.XLS
  • Download Sample Code

using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load workbook
            IWorkbook workbook = new XSSFWorkbook(new FileStream("../../../Data/Sample.xlsx", FileMode.Open));

            //Get the first sheet
            ISheet sheet = workbook.GetSheetAt(0);

            //Set the style of the cell
            ICellStyle style = workbook.CreateCellStyle();
            IFont font = workbook.CreateFont();
            font.Underline = FontUnderlineType.Single;
            font.Color = HSSFColor.Red.Index;
            font.FontHeight = 15;
            style.SetFont(font);

            //Add an URL link
            ICell cell = sheet.CreateRow(1).CreateCell(1);
            cell.SetCellValue("Url link");
            XSSFHyperlink UrlLink = new XSSFHyperlink(HyperlinkType.Url)
            {
                Address = "https://www.e-iceblue.com/"
            };
            cell.Hyperlink = (UrlLink);
            cell.CellStyle = (style);

            //Add an e-mail link
            cell = sheet.CreateRow(3).CreateCell(1);
            cell.SetCellValue("Email link");
            XSSFHyperlink MailLink = new XSSFHyperlink(HyperlinkType.Email)
            {
                Address = "mailto:support@e-iceblue.com"
            };
            cell.Hyperlink = (MailLink);
            cell.CellStyle = (style);

            //Add an external file link
            cell = sheet.CreateRow(5).CreateCell(1);
            cell.SetCellValue("External file link");
            XSSFHyperlink FileLink = new XSSFHyperlink(HyperlinkType.File)
            {
                Address = "ExternalFile.xlsx"
            };
            cell.Hyperlink = (FileLink);
            cell.CellStyle = (style);

            //Save the file
            FileStream file = File.Create("ExcelHyperlink.xlsx");
            workbook.Write(file);
            file.Close();

            //Launch
            System.Diagnostics.Process.Start("ExcelHyperlink.xlsx");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load workbook
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("../../../Data/Sample.xlsx");           

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Set the style of the cell
            CellStyle style = workbook.Styles.Add("Style");
            style.Font.Color = Color.Red;
            style.Font.Underline = FontUnderlineType.Single;
            style.Font.Size = 15;
           
            //Add a URL link
            CellRange range = sheet.Range["B2"];            
            HyperLink UrlLink = sheet.HyperLinks.Add(range);
            UrlLink.TextToDisplay = "Url link";
            UrlLink.Type = HyperLinkType.Url;
            UrlLink.Address = "https://www.e-iceblue.com/";
            range.CellStyleName = style.Name;

            //Add an e-mail link
            range = sheet.Range["B4"];
            HyperLink MailLink = sheet.HyperLinks.Add(range);
            MailLink.TextToDisplay = "Email link";
            MailLink.Type = HyperLinkType.Url;
            MailLink.Address = "mailto:support@e-iceblue.com";
            range.CellStyleName = style.Name;

            //Add an external file link
            range = sheet.Range["B6"];
            HyperLink FileLink = sheet.HyperLinks.Add(range);
            FileLink.TextToDisplay = "External file link";
            FileLink.Type = HyperLinkType.File;
            FileLink.Address = "ExternalFile.xlsx";
            range.CellStyleName = style.Name;

            //Save and Launch
            workbook.SaveToFile("ExcelHyperlink.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("ExcelHyperlink.xlsx");

        }
    }
}