在Excel或OpenOffice中插入超链接
如何添加超链接
Aspose.Cells for Python via .NET允许开发人员使用API或设计者电子表格(手动创建超链接并使用Aspose.Cells for Python via .NET将它们导入其他电子表格)向Excel文件添加超链接。
Aspose.Cells for Python via .NET提供了一个类Workbook,表示Microsoft Excel文件。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供不同方法来向Excel文件添加不同的超链接。
如何添加到URL的链接
Worksheet类包含hyperlinks集合。hyperlinks集合中的每个项目表示Hyperlink。通过调用hyperlinks集合的add方法向URL添加超链接。add方法需要以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,该超链接范围的列数
- URL,URL地址。
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the first 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") |
如何添加到同一文件中某单元格的链接
通过调用hyperlinks集合的add方法,可以将超链接添加到同一Excel文件中的单元格。add方法适用于内部和外部超链接。重载方法的一个版本需要以下参数:
- 单元格名称,将要添加超链接的单元格名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标单元格的地址。
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
workbook.worksheets.add() | |
# Obtaining the reference of the first (default) 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") |
如何添加到外部文件的链接
通过调用hyperlinks集合的add方法,可以将超链接添加到外部Excel文件。add方法需要以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标外部Excel文件的地址。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
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") |