Managing Hyperlinks in a Worksheet
Adding Hyperlinks
To add a hyperlink to a cell using Aspose.Cells.GridDesktop, please follow the steps below:
- Add Aspose.Cells.GridDesktop control to your Form
- Access any desired Worksheet
- Access a desired Cell in the worksheet that will be hyperlinked
- Add some value to the cell to be hyperlinked
- Add Hyperlink to the worksheet by specifying the cell name on which the hyperlink would be applied
Hyperlinks collection in the Worksheet object provides an overloaded Add method. Developers can use any overloaded version of Add method according to their specific needs.
Below code will add a hyperlink to B2 and C3 cells of the worksheet.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Accessing cell of the worksheet | |
GridCell cell = sheet.Cells["b2"]; | |
GridCell cell2 = sheet.Cells["c3"]; | |
// Modifying the width of the column of the cell | |
sheet.Columns[cell.Column].Width = 160; | |
sheet.Columns[cell2.Column].Width = 160; | |
// Adding a value to the cell | |
cell.Value = "Aspose Home"; | |
cell2.Value = "Aspose Home"; | |
// Adding a hyperlink to the worksheet containing cell name and the hyperlink URL with which the cell will be linked | |
sheet.Hyperlinks.Add("b2", "www.aspose.com"); | |
sheet.Hyperlinks.Add("c3", "www.aspose.com"); |
Accessing Hyperlinks
Once a hyperlink will be added to a cell, it may also be required to access and modify the hyperlink at runtime. To do so, developers can simply access the hyperlink from the Hyperlinks collection of the Worksheet by specifying the cell (using cell name or its location in terms of row and column number) to which the hyperlink is added. Once the hyperlink is accessed, developers can modify its URL at runtime.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Accessing a hyperlink added to "c3,b2" cells (specified using its row & column number) | |
Aspose.Cells.GridDesktop.Data.GridHyperlink hyperlink1 = sheet.Hyperlinks[2, 2]; | |
Aspose.Cells.GridDesktop.Data.GridHyperlink hyperlink2 = sheet.Hyperlinks[1, 1]; | |
if (hyperlink1 != null && hyperlink2 != null) | |
{ | |
// Modifying the Url of the hyperlink | |
hyperlink1.Url = "www.aspose.com"; | |
hyperlink2.Url = "www.aspose.com"; | |
MessageBox.Show("Hyperlinks are accessed and URL's are: \n" + hyperlink1.Url + "\n" + hyperlink2.Url); | |
} | |
else | |
{ | |
MessageBox.Show("No hyperlinks are found in sheet. Add hyperlinks first."); | |
} |
Removing Hyperlinks
To remove an existing hyperlink, developers can simply access a desired worksheet and then Remove hyperlink from the Hyperlinks collection of the Worksheet by specifying the hyperlinked cell (using its name or row & column number).
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
if (sheet.Hyperlinks.Count > 0) | |
{ | |
// Removing hyperlink from "c3" cell | |
sheet.Hyperlinks.Remove(2, 2); | |
MessageBox.Show("Hyperlink in C3 cell has been removed."); | |
} | |
else | |
{ | |
MessageBox.Show("No hyperlinks are found in sheet to remove. Add hyperlinks first."); | |
} |