In DsExcel, 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.
Hyperlinks can be configured using the following properties of the IHyperlink interface.
Link To | Address | SubAddress |
---|---|---|
External File | Example: "C:\Users\Desktop\test.xlsx" | null |
Webpage | Example: "https://developer.mescius.com/" | null |
A range in this document | Example: null | "Sheet1!$C$3:$E$4" |
Email Address | Example: "mailto: abc.xyz@mescius.com" | null |
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.
C# |
Copy Code |
---|---|
//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"); |
C# |
Copy Code |
---|---|
// Add a hyperlink to web page //Add a Shape IShape picture = worksheet.Shapes.AddPicture(@"Images\mescius-logo.jpg", 1, 1, 100, 100); //Add Hyperlink worksheet.Hyperlinks.Add(picture, "https://developer.mescius.com/", null , "Click to Open", "MESCIUS, Inc."); //Save to an excel file workbook.Save("ShapeHyperlink.xlsx"); |
C# |
Copy Code |
---|---|
// 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"); |
C# |
Copy Code |
---|---|
//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@mescius.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.
C# |
Copy Code |
---|---|
//Delete hyperlink. //Add Shape IShape shapeOval = worksheet.Shapes.AddShape(AutoShapeType.Oval, 1, 1, 200, 100); // Create Hyperlinks IHyperlink hyperlink1 = worksheet.Hyperlinks.Add(shapeOval, "https://developer.mescius.com/", null, "Click to Open", "MESCIUS, Inc."); //Delete hyperlink1. hyperlink1.Delete(); //Save to an excel file workbook.Save("DeleteHyperlink.xlsx"); |