Manage Hyperlinks in Worksheet
Working with Hyperlinks
Types of Hyperlinks
Generally,the following hyperlinks are supported by Aspose.Cells.GridWeb:
- URL hyperlinks, hyperlinks that can be linked to web URLs.
- Text hyperlinks, URL hyperlinks applied to text.
- Image hyperlinks, URL hyperlinks applied to images.
- Cell command hyperlinks, hyperlinks that post data to a server. Such hyperlinks act more like a button that triggers a server-side event when clicked.
The below sections describe the use of all types of hyperlinks in detail. It also discusses how to access or remove links.
Adding Hyperlinks
URL Hyperlinks
URL hyperlinks look more like simple hyperlinks that you normally see on websites. A URL hyperlink works like an anchor in a cell. Whenever it is clicked, it navigates to a web page or opens a new browser window.
There are different types of URL hyperlinks:
- Text hyperlinks.
- Image hyperlinks.
Developers can specify an image for the hyperlink. If an image isn’t specified, a text hyperlink is created; else an image hyperlink is created.
Text Hyperlinks
To add a text hyperlink to a worksheet:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell in the worksheet.
- Set the text that will be shown in the cell.
- Set the hyperlink’s URL.
- Set the hyperlink’s target,if desired.
- Set a tool tip, if desired.
The example below adds two hyperlinks to a worksheet. One has no target while the other is set to _parent.
Output: text hyperlinks added to worksheet
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Adds a text hyperlink that goes to Aspose site and opens in new window | |
int linkIndex = sheet.Hyperlinks.Add("B1", "http://www.aspose.com"); | |
GridHyperlink link1 = sheet.Hyperlinks[linkIndex]; | |
link1.Target = "_blank"; | |
// Setting text and tool tip of the hyperlink | |
link1.TextToDisplay = "Aspose"; | |
link1.ScreenTip = "Open Aspose Web Site in new window"; | |
// Adding hyperlink to the worksheet to open in parent window | |
linkIndex = sheet.Hyperlinks.Add("B2", "http://www.aspose.com/docs/display/cellsnet/Aspose.Cells.GridWeb"); | |
GridHyperlink link2 = sheet.Hyperlinks[linkIndex]; | |
link2.Target = "_parent"; | |
// Setting text and tool tip of the hyperlink | |
link2.TextToDisplay = "Aspose.Grid Docs"; | |
link2.ScreenTip = "Open Aspose.Grid Docs in parent window"; |
Image Hyperlinks
To add an image hyperlink:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell.
- Set the URL of the image that will be displayed as hyperlink.
- Set the hyperlink URL.
- Set a tool tip, if desired.
- Set the hyperlink text, if desired.
Output: image hyperlinks added to worksheet
Setting the image hyperlink’s AltText fills a similar function as setting an
The image for the image URL could not be found
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Adding hyperlink to the worksheet | |
int linkIndex = sheet.Hyperlinks.Add("B5", "http://www.aspose.com"); | |
GridHyperlink link1 = sheet.Hyperlinks[linkIndex]; | |
link1.Target = "_blank"; | |
// Setting Image URL and tool tip of hyperlink | |
link1.ImageURL = "../Images/Aspose.Banner.gif"; | |
link1.ScreenTip = "Open Aspose Web Site in new window"; | |
// Adding hyperlink to the worksheet | |
linkIndex = sheet.Hyperlinks.Add("B6", "http://www.aspose.com/docs/display/cellsnet/Aspose.Cells.GridWeb"); | |
GridHyperlink link2 = sheet.Hyperlinks[linkIndex]; | |
link2.Target = "_blank"; | |
// Setting URL, tool tip and alt text of hyperlink | |
link2.ImageURL = "../Images/Aspose.Grid.gif"; | |
link2.ScreenTip = "Open Aspose.Grid Docs in new window"; | |
link2.AltText = "Open Aspose.Grid Docs in new window"; | |
// Resize the rows to display image nicely | |
sheet.Cells.SetRowHeight(4, 40); | |
sheet.Cells.SetRowHeight(5, 40); |
Cell Command Hyperlinks
A cell command hyperlink is a special type of hyperlink which triggers a server-side event instead of opening a web page. Developers can add code to the server-side event and perform any task when the hyperlink is clicked. This feature enables developers to create more interactive applications.
To add a cell command hyperlink:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell.
- Set the hyperlink’s Command to any desired value. The value is used by the hyperlink’s event handler to recognise it.
- Set a tool tip, if desired.
- Set the URL for the Image that will be displayed as a hyperlink.
A cell command hyperlink has been added to worksheet
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Adding hyperlink to the worksheet | |
int linkIndex = sheet.Hyperlinks.Add("B8", ""); | |
GridHyperlink link1 = sheet.Hyperlinks[linkIndex]; | |
// Setting the cell command, tool tip and image URL for the hyperlink | |
link1.Command = "Click"; | |
link1.ScreenTip = "Click Here"; | |
link1.ImageURL = "../Images/button.jpg"; | |
// Resize the row to display image nicely | |
sheet.Cells.SetRowHeight(7, 30); |
Event Handling of Cell Command Hyperlinks
Developers need to create an event handler for the GridWeb control’s CellCommand event to perform specific tasks when a specific cell command hyperlink is clicked. The CellCommand event’s event handler provides an object of the CellEventArgs type that offers the Argument property. Use the Argument property to identify a specific hyperlink by comparing its CellCommand value.
The example below creates an event handler for the cell command hyperlink created in the code above. The hyperlink’s CellCommand was set to Click. So, in the event handler, first check it and then add code which displays a message in the A6 cell.
The event handler is invoked when the hyperlink is clicked.
Output: text added to A6 cell when hyperlink is clicked
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Event Handler for CellCommand event | |
protected void GridWeb1_CellCommand(object sender, Aspose.Cells.GridWeb.CellEventArgs e) | |
{ | |
// Checking the CellCommand of the hyperlink | |
if (e.Argument.Equals("Click")) | |
{ | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Adding value to "C8" cell | |
sheet.Cells["C8"].PutValue("Cell Command Hyperlink Clicked"); | |
// Resize the column to display message nicely | |
sheet.Cells.SetColumnWidth(2, 250); | |
} | |
} |
Accessing Hyperlinks
To access an existing hyperlink:
- Access the cell that contains it.
- Get the cell reference.
- Pass the reference to the Hyperlinks collection’s GetHyperlink method to access the hyperlink.
- Modify the hyperlink’s properties.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Accessing a specific cell that contains hyperlink | |
GridCell cell = sheet.Cells["B1"]; | |
// Accessing the hyperlink from the specific cell | |
GridHyperlink link = sheet.Hyperlinks.GetHyperlink(cell); | |
if (link != null) | |
{ | |
// Modifying the text and URL of hyperlink | |
link.TextToDisplay = "Aspose.Blogs"; | |
link.Address = "http://www.aspose.com/Community/Blogs"; | |
} |
Removing Hyperlinks
To remove a hyperlink:
- Access the active worksheet.
- Remove a hyperlink using the Hyperlinks collection’s Remove method.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Removing hyperlink from the specific cell | |
sheet.Hyperlinks.Remove(new Data.GridCellArea() {StartRow = 0, EndRow = 0, StartColumn = 1, EndColumn = 1}); | |