[]
        
(Showing Draft Content)

Hyperlink on Shape

In GcExcel, hyperlinks can be added to various shape types like basic shapes, charts, connectors, pictures and group shapes. It allows users to quickly navigate to related information on a webpage, external file, specific range in the same workbook, or email address by clicking on the shape.

Note: Hyperlink cannot be added to Comment and Slicer shape types.

Hyperlinks can be configured using the following properties of the IHyperlink interface.

  1. The Address and SubAddress properties of the IHyperlink interface can be used to configure the hyperlink references. The table shown below illustrates both the properties with examples:

    Link To

    Address

    SubAddress

    External File

    Example: "C:\Users\Desktop\test.xlsx"

    null

    Webpage

    Example: "http://www.grapecity.com.cn/"

    null

    A range in this document

    Example: null

    "Sheet1!$C$3:$E$4"

    Email Address

    Example: "mailto: abc.xyz@grapecity.com"

    null

  2. The EmailSubject property can be used to set the text of hyperlink's email subject line.

  3. The ScreenTip property can be used to set the tip text for the specified hyperlink.

  4. The TextToDisplay property can be used to set the text to be displayed for the specified hyperlink.

A user can add hyperlink to a shape in a worksheet using the Add method of the IHyperLinks interface.

Refer to the following example code to insert hyperlinks on shapes to redirect to an external file, webpage, range within the worksheet and email address.

//Add a hyperlink to external file

//Add a Shape
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Oval, 1, 1, 200, 100);
shape.TextFrame.TextRange.Paragraphs.Add("Link to Test.xlsx file");
//Add Hyperlink
worksheet.Hyperlinks.Add(shape, @"C:\Test.xlsx", null, "Link to Test.xlsx file", "Test.xlsx");
//Save to an excel file
workbook.Save("ExternalHyperlink.xlsx");
// Add a hyperlink to web page

//Add a Shape
IShape picture = worksheet.Shapes.AddPicture(@"Images\grapecity-logo.jpg", 1, 1, 100, 100);
//Add Hyperlink
worksheet.Hyperlinks.Add(picture, "https://www.grapecity.com.cn/", null , "Click to Open", "GrapeCity");
//Save to an excel file
workbook.Save("ShapeHyperlink.xlsx");
// Create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();
// Fetch a worksheet
IWorksheet worksheet = workbook.Worksheets[0];
// Add another worksheet
IWorksheet worksheet1 = workbook.Worksheets.Add();
#region HyperlinkRange
//Add a hyperlink to a range in Sheet2
//Add a Shape in Sheet1
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Oval, 1, 1, 200, 100);
shape.TextFrame.TextRange.Paragraphs.Add("Go To sheet2 J3:K4");
//Add Hyperlink in Sheet1 which navigates to range J3:K4 in Sheet2
worksheet.Hyperlinks.Add(shape, null, "Sheet2!$J$3:$K$4", "Go To sheet2 J3:K4");
//Save to an excel file
workbook.Save("RangeHyperlink.xlsx");
//Add a hyperlink to email address.

//Add a Shape
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Oval, 1, 1, 200, 100);
shape.TextFrame.TextRange.Paragraphs.Add("Send Feedback");
//Add Hyperlink
worksheet.Hyperlinks.Add(shape, "mailto:web_feedback@grapecity.com", null, "Send your valuable feedback.", "Feedback");
//Save to an excel file
workbook.Save("MailTo.xlsx");

The hyperlink on the shape can be removed using the Delete method of the IHyperlink interface.

Refer to the following example code to delete hyperlink.

//Delete hyperlink.

//Add Shape
IShape shapeOval = worksheet.Shapes.AddShape(AutoShapeType.Oval, 1, 1, 200, 100);

// Create Hyperlinks
IHyperlink hyperlink1 = worksheet.Hyperlinks.Add(shapeOval, "https://www.grapecity.com.cn/", null, "Click to Open", "GrapeCity");

//Delete hyperlink1.
hyperlink1.Delete();

//Save to an excel file
workbook.Save("DeleteHyperlink.xlsx");