管理工作表中的超链接
Contents
[
Hide
]
使用Aspose.Cells.GridDesktop,还可以向工作表的单元格中存储的简单值添加超链接。假设在某些单元格中,您可能有一些值,您希望将其与网页上的更详细信息进行链接。在这种情况下,将希望为该单元格添加一个超链接,以便如果用户点击该单元格,则会被引导到该网页。在本主题中,我们将解释开发人员如何向其工作表中添加和操纵超链接。
添加超链接
使用Aspose.Cells.GridDesktop向单元格添加超链接,请按以下步骤操作:
- 向您的表单中添加Aspose.Cells.GridDesktop控件
- 访问任何所需的工作表
- 访问工作表中将要设置超链接的所需单元格
- 在要设置超链接的单元格中添加一些值
- 通过指定应用超链接的单元格名称向工作表添加超链接
工作表对象中的超链接集合提供了多载入的添加方法。开发人员可以根据特定需求使用任何多载入版本的添加方法。
下面的代码将在工作表的B2和C3单元格中添加超链接。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
访问超链接
一旦向单元格添加了超链接,可能还需要在运行时访问和修改超链接。为此,开发人员可以通过指定已添加超链接的单元格(使用单元格名称或行和列号来表示位置)简单地访问工作表的超链接集合中的超链接。一旦访问超链接,开发人员可以在运行时修改其URL。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |
删除超链接
要删除现有的超链接,开发人员可以简单地访问所需的工作表,然后通过指定超链接单元格的名称或行列号,从工作表的超链接集合中删除超链接。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |
如果要向单元格添加超链接并希望在单元格中显示超链接URL而不是某个值,则不向单元格添加任何值,只需向该单元格添加超链接即可。这样做,单元格将成为超链接,其超链接URL也将显示为其值。