在Excel或OpenOffice中插入超链接
添加超链接
Aspose.Cells允许开发者使用API或设计表格(手动创建超链接并由Aspose.Cells导入到其他表格中)向Excel文件中添加超链接。
Aspose.Cells提供了一个类Workbook,它代表一个Microsoft Excel文件。Workbook类包含一个WorksheetCollection,可以访问Excel文件中的每个工作表。一个工作表由Worksheet类表示。Worksheet类提供了不同的方法,用于向Excel文件添加不同的超链接。
添加指向URL的链接
Worksheet类包含一个getHyperlinks()集合。每个getHyperlinks()中的项目代表一个Hyperlink。通过调用Hyperlinks集合的add方法,可以将超链接添加到URL。该add方法参数如下:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,URL地址。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
let workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the first worksheet | |
let worksheet = workbook.getWorksheets().get(0); | |
// Adding a hyperlink to a URL at "A1" cell | |
worksheet.getHyperlinks().add("A1", 1, 1, "http://www.aspose.com"); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "output.out.xls")); |
将链接添加到同一文件中的单元格
可以通过调用Hyperlinks集合的add方法,在同一个Excel文件中的单元格添加超链接。add方法适用于内部和外部超链接。重载方法之一的参数如下:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标单元格的地址。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
let workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Workbook object | |
workbook.getWorksheets().add(); | |
// Obtaining the reference of the first (default) worksheet | |
let worksheet = workbook.getWorksheets().get(0); | |
// Adding an internal hyperlink to the "B3" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.getHyperlinks().add("B3", 1, 1, "Sheet2!B9"); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "output.out.xls")); |
向外部文件添加链接
可以通过调用Hyperlinks集合的add方法,为外部Excel文件添加超链接。其参数如下:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标外部Excel文件的地址。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.getHyperlinks().add("A5", 1, 1, path.join(dataDir, "book1.xls")); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "output.out.xls")); |