Insert Hyperlinks into Excel or OpenOffice with C++
Adding Hyperlinks
Aspose.Cells allows developers to add hyperlinks to Excel files either using the API or designer spreadsheets (spreadsheets where hyperlinks are created manually and Aspose.Cells is used to import them into other spreadsheets).
Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides different methods for adding different hyperlinks to Excel files.
Adding Link to a URL
The Worksheet class contains a GetHyperlinks() collection. Each item in the GetHyperlinks() collection represents a Hyperlink. Add hyperlinks to URLs by calling the Hyperlinks collection’s Add method. The Add method takes the following parameters:
- Cell name, the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range.
- URL, the URL address.
#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();
}
Adding a Link to a Cell in the Same File
It is possible to add hyperlinks to cells in the same Excel file by calling the Hyperlinks collection’s Add method. The Add method works for both internal and external hyperlinks. One version of the overloaded method takes the following parameters:
- Cell name, the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range.
- URL, the address of the target cell.
#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();
}
Adding a Link to an External File
It is possible to add hyperlinks to external Excel files by calling the Hyperlinks collection’s Add method. The Add method takes the following parameters:
- Cell name, the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range.
- URL, the address of the target, external Excel file.
#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();
}