Back to Devexpress

Use the Excel Export API to Add a Hyperlink to a Picture

officefileapi-115424-excel-export-library-pictures-how-to-add-a-hyperlink-to-a-picture.md

latest3.2 KB
Original Source

Use the Excel Export API to Add a Hyperlink to a Picture

  • Sep 19, 2023
  • 2 minutes to read

This example demonstrates how to associate a hyperlink with a picture. Such hyperlinks are represented by the XlPictureHyperlink objects, which inherit basic hyperlink properties from the XlHyperlinkBase class.

To add a hyperlink to a picture, do the following.

  1. Create a picture to which the hyperlink should be attached.
  2. Access the XlPictureHyperlink object from the IXlPicture.HyperlinkClick property.
  3. Use the XlPictureHyperlink object’s XlHyperlinkBase.TargetUri property to specify the destination to which the hyperlink should refer.
  4. Add a tooltip to your hyperlink, if required. To do this, assign the tooltip text to the XlHyperlinkBase.Tooltip property. This text will be displayed when the cursor hovers over the picture.

View Example

csharp
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);

// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream))
{
    document.Options.Culture = CultureInfo.CurrentCulture;

    // Create a worksheet.
    using (IXlSheet sheet = document.CreateSheet())
    {

        // Load a picture from a file and add a hyperlink to the picture.
        using (IXlPicture picture = sheet.CreatePicture())
        {
            picture.SetImage(Image.FromFile(Path.Combine(imagesPath, "DevExpress.png")), ImageFormat.Png);
            picture.HyperlinkClick.TargetUri = "https://www.devexpress.com/";
            picture.HyperlinkClick.Tooltip = "Developer Express Inc.";
            picture.SetTwoCellAnchor(new XlAnchorPoint(1, 1, 0, 0), new XlAnchorPoint(10, 5, 2, 15), XlAnchorType.TwoCell);
        }
    }
}
vb
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)

' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
    document.Options.Culture = CultureInfo.CurrentCulture

    ' Create a worksheet.
    Using sheet As IXlSheet = document.CreateSheet()

        ' Load a picture from a file and add a hyperlink to the picture.
        Using picture As IXlPicture = sheet.CreatePicture()
            picture.SetImage(Image.FromFile(Path.Combine(imagesPath, "DevExpress.png")), ImageFormat.Png)
            picture.HyperlinkClick.TargetUri = "https://www.devexpress.com/"
            picture.HyperlinkClick.Tooltip = "Developer Express Inc."
            picture.SetTwoCellAnchor(New XlAnchorPoint(1, 1, 0, 0), New XlAnchorPoint(10, 5, 2, 15), XlAnchorType.TwoCell)
        End Using
    End Using
End Using