在Excel或OpenOffice中插入超链接的C++方法
添加超链接
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地址。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Add a hyperlink to cell "A1"
worksheet.GetHyperlinks().Add(u"A1", 1, 1, u"http://www.aspose.com");
// Save the workbook
workbook.Save(outputFilePath);
std::cout << "Hyperlink added successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
将链接添加到同一文件中的单元格
可以通过调用 Hyperlinks 集合的 Add 方法,为相同Excel文件中的单元格添加超链接。该 Add 方法适用于内部和外部超链接。重载方法的一个版本接受以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标单元格的地址。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Instantiating a Workbook object
Workbook workbook;
// Adding a new worksheet to the Workbook object
workbook.GetWorksheets().Add();
// Obtaining the reference of the first (default) worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in
// The same Excel file
worksheet.GetHyperlinks().Add(u"B3", 1, 1, u"Sheet2!B9");
// Saving the Excel file
workbook.Save(outDir + u"output.out.xls");
std::cout << "Hyperlink added successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
向外部文件添加链接
可以通过调用 Hyperlinks 集合的 Add 方法,为外部Excel文件添加超链接。该方法接受以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- URL,目标外部Excel文件的地址。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Add a new worksheet to the workbook
int i = workbook.GetWorksheets().Add();
// Get the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.GetWorksheets().Get(i);
// Add an internal hyperlink to the "A5" cell of the other worksheet "Sheet2" in the same Excel file
worksheet.GetHyperlinks().Add(U16String(u"A5"), 1, 1, srcDir + U16String(u"book1.xls"));
// Save the Excel file
workbook.Save(outDir + U16String(u"output.out.xls"));
std::cout << "Hyperlink added successfully!" << std::endl;
Aspose::Cells::Cleanup();
}