Add Hyperlinks to Cells
Contents
[
Hide
]
Aspose.Cells for .NET allows you to perform almost any tasks through your application that a user can perform in Microsoft Excel. This article compares how to add a hyperlink to a cell in a worksheet using VSTO and Aspose.Cells for .NET.
Adding Hyperlinks to Cells
To add hyperlinks to cells in a spreadsheet, take the following steps:
- Set up the worksheet:
- Instantiate an Application object. (VSTO only.)
- Add a Workbook.
- Get the first sheet.
- Add text to the cells that you’ll add a hyperlink to.
- Add hyperlink.
- Save the document.
These steps are shown in the code examples below. The first examples shows how to use VSTO with either C# or Visual Basic to add a hyperlink to a cell. The examples that follow show how to do the same thing using Aspose.Cells for .NET, again using C# or Visual Basic.
The code samples generate an Excel file that has a hyperlink in cell A1 on the first worksheet.
A hyperlink is added to cell A1.
Adding Hyperlinks to Cells with VSTO
C#
.......
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Reflection;
.......
//Instantiate the Application object.
Excel.ApplicationClass ExcelApp = new Excel.ApplicationClass();
//Add a Workbook.
Excel.Workbook objBook = ExcelApp.Workbooks.Add(System.Reflection.Missing.Value);
//Get the First sheet.
Excel.Worksheet objSheet = (Excel.Worksheet)objBook.Sheets["Sheet1"];
//Define a range object(A1).
Excel.Range _range;
_range = objSheet.get_Range("A1", "A1");
//Add a hyperlink to it.
objSheet.Hyperlinks.Add(_range, "http://www.aspose.com/", Type.Missing, "Click to go to Aspose site", "Aspose Site!");
//Save the excel file.
objBook.SaveCopyAs("c:\\Hyperlink_test.xls");
//Quit the Application.
ExcelApp.Quit();
Adding Hyperlinks to Cells with Aspose.Cells for .NET
C#
.......
using Aspose.Cells;
.......
//Instantiate a new Workbook object.
Workbook workbook = new Workbook();
//Get the First sheet.
Worksheet worksheet = workbook.Worksheets[0];
//Define A1 Cell.
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
//Add a hyperlink to it.
int index = worksheet.Hyperlinks.Add("A1", 1, 1, "http://www.aspose.com/");
worksheet.Hyperlinks[index].TextToDisplay = "Aspose Site!";
worksheet.Hyperlinks[index].ScreenTip = "Click to go to Aspose site";
//Save the excel file.
workbook.Save("c:\\Hyperlink_test.xls");