将超链接插入 Excel 或 OpenOffice
Contents
[
Hide
]
超链接用于在两个实体之间创建链接。每个人都熟悉超链接的使用,尤其是在网站上。
使用 Aspose.Cells,开发人员可以在 Microsoft Excel 文件中创建不同种类的超链接。本主题讨论 Aspose.Cells 支持哪些类型的超链接以及如何在我们的 Excel 文件中使用它们。
添加超链接
Aspose.Cells 允许开发人员使用 API 或设计器电子表格(手动创建超链接的电子表格,Aspose.Cells 用于将它们导入其他电子表格)向 Excel 文件添加超链接。
Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了向 Excel 文件添加不同超链接的不同方法。
将链接添加到 URL
这工作表类包含一个超级链接收藏。中的每一项超级链接集合代表一个超级链接.通过调用超级链接收藏的添加方法。这添加方法采用以下参数:
- Cell name,超链接将添加到的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围内的列数
- 网址,网址地址。
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a hyperlink to a URL at "A1" cell | |
worksheet.Hyperlinks.Add("A1", 1, 1, "http:// Www.aspose.com"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
在上面的示例中,超链接被添加到空单元格中的 URL,A1.在这种情况下,如果单元格为空,则 URL 地址也会作为其值添加到该空单元格中。如果单元格不为空并且添加了超链接,则单元格的值看起来像纯文本。要使其看起来像超链接,请在该单元格上应用适当的格式设置。
在同一文件中添加指向 Cell 的链接
可以通过调用超级链接收藏的添加方法。这添加方法适用于内部和外部超链接。重载方法的一种版本采用以下参数:
- Cell name,要添加超链接的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围内的列数。
- 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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
workbook.Worksheets.Add(); | |
// Obtaining the reference of the first (default) worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.Hyperlinks.Add("B3", 1, 1, "Sheet2!B9"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
添加指向外部文件的链接
可以通过调用超级链接收藏的添加方法。这添加方法采用以下参数:
- Cell name,超链接将添加到的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围内的列数。
- URL,目标地址,外部Excel文件。
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.Hyperlinks.Add("A5", 1, 1, dataDir + "book1.xls"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |