如何在 C# 中使用 Excel 创建超链接

How to Create Hyperlink

This article was translated from English: Does it need improvement?
Translated
View the article in English

Excel hyperlinks provide clickable references to locations within the workbook, different files, web pages, or email addresses. They enhance navigation, allowing quick access to related information and external resources. Hyperlinks create interactive and user-friendly spreadsheets, facilitating easy access to additional data or external content.

IronXL enables the creation of hyperlinks for URLs, opening external files from both local and FTP (File Transfer Protocol) file systems, email addresses, cell addresses, and defined name cells without the use of Interop in .NET C#.

Quickstart: Add a Hyperlink with IronXL in Just a Few Steps

This example shows how easy it is to add a web hyperlink to an Excel cell using IronXL. In just a couple of lines––create the workbook, set the cell value, assign the hyperlink, and save––you’ll have an interactive link in your spreadsheet.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    IronXL.WorkBook workbook = IronXL.WorkBook.Create(IronXL.ExcelFileFormat.XLSX);
    workbook.DefaultWorkSheet.GetCellAt(0, 0).Value = "Visit IronXL Docs";
    workbook.DefaultWorkSheet.GetCellAt(0, 0).Hyperlink = "https://ironsoftware.com";
    workbook.SaveAs("hyperlink_quick.xlsx");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

Get started with IronXL

今天在您的项目中使用 IronXL,免费试用。

第一步:
green arrow pointer


The Hyperlink property exists in the Cell class. The worksheet["A1"] code returns a Range object, you can use the First method to access the first cell in the range.

Alternatively, you can directly access the cell using the GetCellAt method, allowing you to access the Hyperlink property directly.

Let's explore an example of creating link hyperlinks. Both HTTP and HTTPS protocols are supported.

警告Using the GetCellAt method to select an unmodified cell will throw System.NullReferenceException: 'Object reference not set to an instance of an object.'

:path=/static-assets/excel/content-code-examples/how-to/hyperlinks-set-link-hyperlink.cs
using IronXL;

WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Modify the cell's property
workSheet["A1"].Value = "Link to ironpdf.com";

// Set hyperlink at A1 to https://ironpdf.com/
workSheet.GetCellAt(0, 0).Hyperlink = "https://ironpdf.com/";

workBook.SaveAs("setLinkHyperlink.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Modify the cell's property
Private workSheet("A1").Value = "Link to ironpdf.com"

' Set hyperlink at A1 to https://ironpdf.com/
workSheet.GetCellAt(0, 0).Hyperlink = "https://ironpdf.com/"

workBook.SaveAs("setLinkHyperlink.xlsx")
$vbLabelText   $csharpLabel

Demonstration

Link Hyperlink

To create a hyperlink to a cell within the same worksheet, simply use the cell's address, such as Z20. However, to create a hyperlink across worksheets, you can use the address convention "worksheetName!address". For example, "Sheet2!A1".

Define name cells can have either workbook(global) or worksheet scope. If you want to create a hyperlink to a defined name within the same worksheet or a defined name with workbook scope, you can specify the name directly. To create a hyperlink for a defined name with worksheet scope on a different worksheet, specify the worksheet name as mentioned above. For example, "Sheet2!Iron".

:path=/static-assets/excel/content-code-examples/how-to/hyperlinks-set-hyperlink-across-worksheet.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet1 = workBook.CreateWorkSheet("Sheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("Sheet2");

// Create workbook(global) define name
workSheet1["D5"].SaveAsNamedRange("Iron", true);

// Create worksheet define name
workSheet2["D10"].SaveAsNamedRange("Hello", false);

// --== Within the same worksheet ==--
// Set hyperlink to cell Z20
workSheet1["A1"].Value = "Z20";
workSheet1["A1"].First().Hyperlink = "Z20";

// Set hyperlink to define name "Iron"
workSheet1["A2"].Value = "Iron";
workSheet1["A2"].First().Hyperlink = "Iron";

// --== Across worksheet ==--
// Set hyperlink to cell A1 of Sheet2
workSheet1["A3"].Value = "A1 of Sheet2";
workSheet1["A3"].First().Hyperlink = "Sheet2!A1";

// Set hyperlink to define name "Hello" of Sheet2
workSheet1["A4"].Value = "Define name Hello of Sheet2";
workSheet1["A4"].First().Hyperlink = "Sheet2!Hello";

workBook.SaveAs("setHyperlinkAcrossWorksheet.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Demonstration

Hyperlink Across Worksheet

In addition to the hyperlink types mentioned earlier, IronXL also supports the creation of FTP, file, and email hyperlinks.

  • FTP: Starting with ftp://
  • File: Specify an absolute path starting with file:///
  • Email: Starting with mailto:

请注意Both FTP and File hyperlinks require the use of absolute paths.

:path=/static-assets/excel/content-code-examples/how-to/hyperlinks-set-other-hyperlink.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set hyperlink to open file sample.xlsx
workSheet["A1"].Value = "Open sample.xslx";
workSheet["A1"].First().Hyperlink = "ftp://C:/Users/sample.xlsx";

// Set hyperlink to open file sample.xlsx
workSheet["A2"].Value = "Open sample.xslx";
workSheet["A2"].First().Hyperlink = "file:///C:/Users/sample.xlsx";

// Set hyperlink to email example@gmail.com
workSheet["A3"].Value = "example@gmail.com";
workSheet["A3"].First().Hyperlink = "mailto:example@gmail.com";

workBook.SaveAs("setOtherHyperlink.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Demonstration

Other Types of Hyperlinks

To remove a hyperlink, simply invoke the RemoveHyperlink method. This method can be accessed from the cell object.

:path=/static-assets/excel/content-code-examples/how-to/hyperlinks-remove-hyperlink.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("setLinkHyperlink.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove hyperlink
workSheet["A1"].First().RemoveHyperlink();

workBook.SaveAs("removeHyperlink.xlsx");
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("setLinkHyperlink.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Remove hyperlink
workSheet("A1").First().RemoveHyperlink()

workBook.SaveAs("removeHyperlink.xlsx")
$vbLabelText   $csharpLabel

常见问题解答

如何使用C#在Excel单元格中添加超链接?

您可以通过将单元格的Hyperlink属性设置为包含所需链接的Uri对象来使用IronXL向Excel单元格添加超链接。这使您能够创建指向网站、电子邮件和文件的可点击引用。

使用.NET库可以在Excel中创建哪些类型的超链接?

使用IronXL,您可以创建到HTTP和HTTPS URL、FTP链接、本地文件路径、电子邮件地址,甚至是同一或不同工作表内的单元格地址的超链接。

如何使用C#创建指向不同工作表单元格的超链接?

要使用IronXL创建指向不同工作表单元格的超链接,可以使用地址格式worksheetName!cellAddress。例如,要链接到Sheet2上的单元格A1,请使用Sheet2!A1

我可以以编程方式从Excel单元格中删除超链接吗?

可以,您可以通过在相应的单元格对象上调用RemoveHyperlink方法来使用IronXL从Excel单元格中删除超链接。

使用IronXL在Excel中设置超链接相较于传统方法有什么优势?

使用IronXL在Excel中设置超链接无需Excel Interop,在.NET应用程序中使过程更快更高效,并且不依赖于服务器上安装Excel。

如何使用C#在Excel文件中创建电子邮件超链接?

要使用IronXL在Excel文件中创建电子邮件超链接,将Hyperlink属性设置为以'mailto:'方案开头的Uri对象,后跟电子邮件地址。

是否可以使用.NET库创建FTP和文件超链接?

是的,IronXL允许您通过以'ftp://'开头和以'file:///'开头的绝对路径创建FTP和文件超链接。

如何使用C#在Excel中创建指向定义的名称单元格的超链接?

您可以通过直接指定具有工作簿范围的定义名称或将工作表名称作为worksheetName!definedName包含的工作表范围定义名称来使用IronXL创建指向定义名称单元格的超链接。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布