Excel is an exceptional tool for working with large data sets, data visualization, and statistical analysis of data. While creating a detailed data report in Excel, certain data may require to be linked to external data sources, webpages, email addresses, and target locations in another worksheet in the same workbook. Microsoft Excel provides all these hyperlinking options.
As a .NET application developer, you may be interested in providing hyperlinking functionality in your C#/VB.NET application. You can achieve this using NPOI API in your application using C# or VB.NET as shown in this article.
How to Add Hyperlinks in Microsoft Excel
Microsoft Excel lets you create a variety of hyperlinks in a workbook. These include:
- Linking text to a URL
- Linking text to an Email address
- Creating a hyperlink to a specific location in a workbook
- Link to an existing file
Before we can write our own code for creating hyperlinks in an Excel file, let us first have a look at how these are created using Microsoft Excel
Steps to Create Hyperlinks using Microsoft Excel
You can create hyperlinks in an Excel file using the following steps.
Create a Link to an Existing File to Webpage
- On a worksheet, click the cell where you want to create a link.
- On the Insert tab, in the Links group, click Link
- Under Link to, click Existing File or Web Page.
- To select a file, click Current Folder, and then click the file that you want to link to.
- You can change the current folder by selecting a different folder in the Look-in list.
- To select a web page, click Browsed Pages and then click the web page that you want to link to.
- To select a file that you recently used, click Recent Files, and then click the file that you want to link to.
- To enter the name and location of a known file or web page that you want to link to, type that information in the Address box.
- To locate a web page, click Browse the Web, open the web page that you want to link to, and then switch back to Excel without closing your browser.
Create a Link to an Email Address
To hyperlink to an email address in Excel, you can follow these steps:
- Open your Excel spreadsheet and navigate to the cell where you want to create the hyperlink.
- Select the cell and right-click on it. Choose “Hyperlink” from the context menu. Alternatively, you can also click on the “Insert” tab in the Excel ribbon, and then click on the “Hyperlink” button.
- The “Insert Hyperlink” dialog box will appear. In the left pane, select “Email Address.”
- In the “Email address” field, enter the email address you want to hyperlink to. For example, you can enter “mailto:example@example.com” (replace “example@example.com” with the desired email address).
- Optionally, you can enter a friendly name or text that will be displayed for the hyperlink in the “Text to display” field. This can be something like “Send Email” or the actual email address itself.
- Click the “OK” button to create the hyperlink.
- The cell will now display the email address as a hyperlink. You can click on it, and your default email client will open with a new email address to the specified email address.
Create a Link to a Specific Location in a Workbook
To create a link to a specific location within a workbook in Excel, you can use the following steps:
- Open your Excel spreadsheet and navigate to the cell or range that you want to link to.
- Right-click on the cell or range and select “Copy” from the context menu. Alternatively, you can press Ctrl+C on your keyboard to copy the cell or range.
- Move to the location where you want to create the link.
- Right-click on the cell where you want to create the link and select “Hyperlink” from the context menu. You can also go to the “Insert” tab in the Excel ribbon and click on the “Hyperlink” button.
- The “Insert Hyperlink” dialog box will appear. In the left pane, select “Place in This Document.”
- In the “Type the cell reference” field, Excel should automatically display the reference to the cell or range you copied in Step 2. If not, you can manually enter the reference in the format “SheetName!CellReference” or “SheetName!RangeReference”. For example, “Sheet1!A1” or “Sheet2!B2:E5”.
- Optionally, you can enter a friendly name or text for the link in the “Text to display” field.
- Click the “OK” button to create the hyperlink.
- The cell will now display the link to the specific location within the workbook. Clicking on it will take you directly to the referenced cell or range.
How to Add Hyperlinks in Excel using C#
Now that we have seen how to add hyperlinks using Microsoft Excel, let us now have a look at how we can do the same in our .NET application. We’ll be using the open-source API NPOI for .NET for this purpose and will write the code in C#.
But before that, we need to install NPOI in our .NET project to get started. You can learn about this in our detailed instructions guide for Installing NPOI for .NET.
//Create a workbook object
IWorkbook wb = new XSSFWorkbook();
//Create a worksheet
ISheet sheet = wb.CreateSheet("Hyperlinks");
//Define Cell style for hyperlinks..by default thee are blue and hyperlinked
ICellStyle hlink_style = wb.CreateCellStyle();
IFont hlink_font = wb.CreateFont();
hlink_font.Underline = FontUnderlineType.Single;
hlink_font.Color = HSSFColor.Blue.Index;
hlink_style.SetFont(hlink_font);
ICell cell;
//URL Hyperlink
cell = sheet.CreateRow(0).CreateCell(0);
cell.SetCellValue("URL Link");
XSSFHyperlink link = new XSSFHyperlink(HyperlinkType.Url);
link.Address = ("https://www.fileformat.com/");
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
//link to a file in the current directory
cell = sheet.CreateRow(1).CreateCell(0);
cell.SetCellValue("File Link");
link = new XSSFHyperlink(HyperlinkType.File);
link.Address = ("FileFormat.xlsx");
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
//e-mail link
cell = sheet.CreateRow(2).CreateCell(0);
cell.SetCellValue("Email Link");
link = new XSSFHyperlink(HyperlinkType.Email);
//note, if subject contains white spaces, make sure they are url-encoded
link.Address = ("mailto:info@fileformat.com?subject=FileForamts");
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
//Create a target sheet and cell
ISheet sheet2 = wb.CreateSheet("Sheet2");
sheet2.CreateRow(0).CreateCell(0).SetCellValue("Target ICell");
cell = sheet.CreateRow(3).CreateCell(0);
cell.SetCellValue("Worksheet Link");
link = new XSSFHyperlink(HyperlinkType.Document);
link.Address = ("'Sheet2'!A1");
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
using (FileStream sw = File.Create("test.xlsx"))
{
wb.Write(sw, false);
}
Conclusion
In this article, we showed how to add different types of hyperlinks in an XLSX file from within your .NET application using NPOI API. You can learn more about using NPOI API for Spreadsheets to use the API in your C#/VB.NET applications. For more examples, stay tuned.