Add or Embed Hyper Link in Excel Using C#.Net, CloseXML | How to

How to add or embed a hyper link on text in your excel file. Here we use List to pass link and text.
In today's article I will show you how you can add a hyper link in on a test in your excel file. This code will demonstrate to embed the link on a text. 

Related Link:

C# Code to Past Image in Excel On Given Cell No, ClosedXML

So first we will install CloseXML NuGet package. 

 Install-Package ClosedXML 
After installing CloseXML NuGet package. We will use the below code to add or embed in you excel file. In this code i have passed excel file path, sheet name where we want to add the text and its hyper link. In this one more important parameter is starting point or the cell no, where you want to add the hett and its hyper link.
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public static void AddLinksToExcel(
    string excelFilePath,
    string sheetName,
    List<dynamic> data,
    string startCellNo)
{
    if (string.IsNullOrWhiteSpace(excelFilePath))
        throw new ArgumentException("Excel file path is required.");

    if (string.IsNullOrWhiteSpace(sheetName))
        throw new ArgumentException("Sheet name is required.");

    if (data == null || data.Count == 0)
        return;

    // Create the directory if it does not exist
    string directory = Path.GetDirectoryName(
        Path.GetFullPath(excelFilePath));

    if (!Directory.Exists(directory))
        Directory.CreateDirectory(directory);

    using (var workbook = File.Exists(excelFilePath)
        ? new XLWorkbook(excelFilePath)
        : new XLWorkbook())
    {
        // Get existing sheet or create a new one
        var worksheet = workbook.Worksheets
            .FirstOrDefault(ws => ws.Name == sheetName);

        if (worksheet == null)
            worksheet = workbook.Worksheets.Add(sheetName);

        // Starting cell
        var startCell = worksheet.Cell(startCellNo);

        int startRow = startCell.Address.RowNumber;
        int startColumn = startCell.Address.ColumnNumber;

        int rowOffset = 0;

        foreach (var item in data)
        {
            // Read val1 and val2 from dynamic object
            string displayText = Convert.ToString(item.val1) ?? "";
            string linkUrl = Convert.ToString(item.val2) ?? "";

            var cell = worksheet.Cell(
                startRow + rowOffset,
                startColumn);

            // Set display text
            cell.Value = displayText;

            // Add hyperlink if URL is provided
            if (!string.IsNullOrWhiteSpace(linkUrl))
            {
                cell.Hyperlink = new XLHyperlink(linkUrl);

                // Optional hyperlink formatting
                cell.Style.Font.Underline =
                    XLFontUnderlineValues.Single;

                cell.Style.Font.FontColor =
                    XLColor.Blue;
            }

            rowOffset++;
        }

        // Save the Excel file
        workbook.SaveAs(excelFilePath);
    }
} 
Here List<dynamic> is a dynamic list in which you need to place your value. Here i have placed value in List as give format.
data.Add(new dynamic{val1="Test url 1", val2="https://wwww.google.com"});
data.Add(new dynamic{val1="Test url 2", val2="https://wwww.google.com"}); 
With the help of this list you place multiple links. If you want to add or append only one link, you can pass only one row or you can pass you own class value and make modification in foreach loop. Now we have done lets run the code and check the output.Hyper Link in Excel

Post a Comment