管理工作表中的超链接
Contents
[
Hide
]
使用 Aspose.Cells.GridDesktop,还可以将超链接添加到存储在工作表单元格中的简单值。假设在某些单元格中,您可能希望将某些值链接到网页上的更详细信息。在这种情况下,最好向该单元格添加一个超链接,这样如果用户单击该单元格,他就会被定向到该网页。在本主题中,我们将解释开发人员如何在工作表中添加和操作超链接。
添加超链接
要使用 Aspose.Cells.GridDesktop 添加到单元格的超链接,请按照以下步骤操作:
- 将 Aspose.Cells.GridDesktop 控件添加到您的形式
- 访问任何想要的工作表
- 访问一个想要的Cell在将被超链接的工作表中
- 向要超链接的单元格添加一些值
- 添加超级链接通过指定要应用超链接的单元格名称到工作表
超级链接收集在工作表对象提供重载添加方法。开发人员可以使用任何重载版本添加方法根据自己的具体需要。
下面的代码将添加一个超链接到B2和C3工作表的单元格。
This file contains hidden or 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 hidden or 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 hidden or 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 也将作为其值显示在单元格中。