This is a migrated thread and some comments may be shown as answers.

How can I Programmatically insert a Hyperlink into a cell?

4 Answers 785 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 13 Oct 2015, 02:43 PM

It's a simple one....

 

How can I Programmatically insert a Hyperlink into a cell?

 

Thanks.​

4 Answers, 1 is accepted

Sort by
0
Aylin
Telerik team
answered on 14 Oct 2015, 12:03 PM
Hello Robert,

Please check the following code snippet which demonstrates how to insert hyperlinks programmatically: 
Worksheet worksheet = this.radSpreadsheet.ActiveWorksheet;
worksheet.Cells[0, 0].SetValueAsText("Telerik");
 
HyperlinkInfo website = HyperlinkInfo.CreateHyperlink("www.telerik.com", "Telerik");
CellIndex a1Index = new CellIndex(0, 0);
worksheet.Hyperlinks.Add(a1Index, website);

You could find more information on that topic at the Hyperlink article from our online documentation.

I hope that helps.

Regards,
Aylin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Robert
Top achievements
Rank 1
answered on 16 Oct 2015, 08:58 AM

Hi Aylin,

 Thank you for your response. I used searched for 'Hyperlink' on the Telerik Spreadsheet Forum and API Documentation and received zero results. I found essentially the same information you presented me with here by searching Google for 'Excel vba insert hyperlink'.

 What we are actually trying to achieve is to format a cell to look like it contains a hyperlink, but when the user clicks on it, we are going to spawn a new view with additional information. We can achieve all the text formatting we need for this in code behind, but we can't change the cursor to a hand, as is customary for a link. So.. I'm trying to insert a hyperinklink...

We are wrapping the RadSpreadsheet in a UserControl, but use Prism MVVM and I'm using Prism's InteractionRequest to spawn a new view when a user clicks on a cell with information that can be elaborated on with additional data. In order to get the style I want, I am simply linking to the same cell in the same worksheet that the user clicks on with:

...

for (int i = 0; i < Accounts.Count; i++) {
 
   // for purposes of our example, data containing links will always be in column 3
   // generate a subaddress equivalent to the same cell that contains the hyperlink
   string alphaRow = Convert.ToChar(i + 65).ToString();
   string cell = alphaRow + "3:" + alphaRow + "3";
   HyperlinkInfo link = HyperlinkInfo.CreateInDocumentHyperlink(cell,
     "Click for more about '" + user.Permissions.ToString() + "'");
 
   Debug.WriteLine("Hyperlink inserted at CellIndex: ({0}:3) Place in Doc: {1} link: {2} ",
     i.ToString(),
     cell,
     user.Permissions.ToString());
}

Debug shows the links pointing to the right place in the document, but I'm still getting some wierd behavior. After clicking, I'm linked to an unexpected location in the document, or I'm still getting an Invalid Reference error.

Any suggestions?

0
Robert
Top achievements
Rank 1
answered on 16 Oct 2015, 01:50 PM

Ok, I simply added an empty string for the cell reference on the HyperlinkInfo object and it works as expected..

    HyperlinkInfo link = HyperlinkInfo.CreateInDocumentHyperlink(string.Empty, "Click for more about '" +
      user.Permissions.ToString() + "'");
    worksheet.Hyperlinks.Add(new CellIndex(i, 3), link);​

0
Anna
Telerik team
answered on 19 Oct 2015, 01:29 PM
Hi,

Using an empty string will indeed work, because it will create a hyperlink which does nothing when clicked. However, something which is important to note is that this will create an invalid document which you will not be able to export. This, of course, will have to be fixed in the future, but I am concerned that it might interfere with your work at the present moment. I am not sure if your scenario will require export of the document. Please, let me know if you have any thoughts on this.

Regards,
Anna
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Spreadsheet
Asked by
Robert
Top achievements
Rank 1
Answers by
Aylin
Telerik team
Robert
Top achievements
Rank 1
Anna
Telerik team
Share this question
or