C++: Add Hyperlinks to Excel

Hyperlinks are a powerful tool in Excel that allows users to connect different parts of an Excel file or link to external sources such as websites, email addresses, or other files. They provide a quick and easy way for users to navigate within a worksheet or between different worksheets. In addition to facilitating navigation, hyperlinks can also provide additional context or resources related to the data in a file. For example, you can link to a website that provides more information about a specific product listed in your worksheet to help your readers gain a deeper understanding of the product. In this article, we will explore how to add hyperlinks to Excel, specifically, how to add text hyperlinks and image hyperlinks to Excel files in C++ using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Add Text Hyperlinks to Excel in C++

A text hyperlink in Excel is a word or phrase that you can click on to jump to a specific location, such as a different part of the Excel file, an email address, a webpage, or an external file. The following steps explain how to add a text hyperlink to an Excel file using Spire.XLS for C++:

  • Initialize an instance of the Workbook class.
  • Get a specific worksheet using the Workbook->GetWorksheets()->Get(int index) method.
  • Get the cell that you want to add a hyperlink to using the Worksheet->GetRange(LPCWSTR_S name) method.
  • Add a hyperlink to the cell using the Worksheet->GetHyperLinks()->Add(intrusive_ptr<IXLSRange> range) method.
  • Set the type, display text, and address for the hyperlink using the XlsHyperLink->SetType(HyperLinkType value), XlsHyperLink->SetTextToDisplay(LPCWSTR_S value) and XlsHyperLink->SetAddress(LPCWSTR_S value) methods.
  • Autofit column width using the XlsWorksheet->AutoFitColumn(int columnIndex) method.
  • Save the result file using the Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
    //Initialize an instance of the Workbook class
    intrusive_ptr<Workbook> workbook = new Workbook();

    //Get the first worksheet
    intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

    //Add a text hyperlink that links to a webpage
    intrusive_ptr<CellRange> cell1 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B3"));
    intrusive_ptr<HyperLink> urlLink = sheet->GetHyperLinks()->Add(cell1);
    urlLink->SetType(HyperLinkType::Url);
    urlLink->SetTextToDisplay(L"Link to a website");
    urlLink->SetAddress(L"https://www.e-iceblue.com/");

    //Add a text hyperlink that links to an email address
    intrusive_ptr<CellRange> cell2 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"E3"));
    intrusive_ptr<HyperLink> mailLink = sheet->GetHyperLinks()->Add(cell2);
    mailLink->SetType(HyperLinkType::Url);
    mailLink->SetTextToDisplay(L"Link to an email address");
    mailLink->SetAddress(L"mailto:support@e-iceblue.com");

    //Add a text hyperlink that links to an external file
    intrusive_ptr<CellRange> cell3 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B7"));
    intrusive_ptr<HyperLink> fileLink = sheet->GetHyperLinks()->Add(cell3);
    fileLink->SetType(HyperLinkType::File);
    fileLink->SetTextToDisplay(L"Link to an external file");
    fileLink->SetAddress(L"C:\\Users\\Administrator\\Desktop\\Report.xlsx");

    //Add a text hyperlink that links to a cell in another sheet
    intrusive_ptr<CellRange> cell4 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"E7"));
    intrusive_ptr<HyperLink> sheetLink = sheet->GetHyperLinks()->Add(cell4);
    sheetLink->SetType(HyperLinkType::Workbook);
    sheetLink->SetTextToDisplay(L"Link to a cell in sheet2");
    sheetLink->SetAddress(L"Sheet2!B5");

    //Add a text hyperlink that links to a UNC address
    intrusive_ptr<CellRange> cell5 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B11"));
    intrusive_ptr<HyperLink> uncLink = sheet->GetHyperLinks()->Add(cell5);
    uncLink->SetType(HyperLinkType::Unc);
    uncLink->SetTextToDisplay(L"Link to a UNC address");
    uncLink->SetAddress(L"\\192.168.0.121");

    //Autofit column widths
    sheet->AutoFitColumn(2);
    sheet->AutoFitColumn(5);

    //Save the result file
    workbook->SaveToFile(L"AddTextHyperlinks.xlsx", ExcelVersion::Version2013);
    workbook->Dispose();
}

C++: Add Hyperlinks to Excel

Add Image Hyperlinks to Excel in C++

Similar to a text hyperlink, an image hyperlink is an image that you can click on to navigate to a specific location. The following steps explain how to add an image hyperlink to an Excel file using Spire.XLS for C++:

  • Initialize an instance of the Workbook class.
  • Get a specific worksheet using the Workbook->GetWorksheets()->Get(int index) method.
  • Add text to a specific cell using the Worksheetsheet->GetRange(LPCWSTR_S name)->SetText(LPCWSTR_S value) method.
  • Add an image to a specific cell using the Worksheet->GetPictures()->Add(int topRow,int leftColumn, LPCWSTR_S fileName) method.
  • Set image width and height.
  • Add a hyperlink to the image using the XlsBitmapShape->SetHyperLink(LPCWSTR_S linkString, bool isExternal) method.
  • Save the result file using the Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
    //Initialize an instance of the Workbook class
    intrusive_ptr<Workbook> workbook = new Workbook();
    //Get the first worksheet
    intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

    //Add text to a specific cell
    sheet->GetRange(L"A1")->SetText(L"Image Hyperlink");

    //Add an image to a specific cell
    intrusive_ptr<ExcelPicture> picture = ExcelPicture::Dynamic_cast<ExcelPicture>(sheet->GetPictures()->Add(2, 1, L"Logo.png"));
    //Set image width and height
    picture->SetWidth(100);
    picture->SetHeight(100);

    //Add a hyperlink to the image
    picture->SetHyperLink(L"https://www.e-iceblue.com", true);

    //Set column width
    sheet->GetColumns()->GetItem(0)->SetColumnWidth(13);

    //Save the result file
    workbook->SaveToFile(L"AddImageHyperlink.xlsx", ExcelVersion::Version2013);
    workbook->Dispose();
}

C++: Add Hyperlinks to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.