Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 808407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:33:24+00:00 2026-05-15T00:33:24+00:00

I can’t seem to find any documentation or code samples on how to add

  • 0

I can’t seem to find any documentation or code samples on how to add a hyperlink to a cell in Excel 2007 using the Open XML SDK 2.0. I am using the following code, but is there a step I am missing?

WorksheetPart workSheetPart = ExcelUtilities.GetWorkSheetPart(mWorkBookPart, "Program");

workSheetPart.AddHyperlinkRelationship(new Uri("http://www.google.com", UriKind.Absolute), true);

workSheetPart.Worksheet.Save();

mWorkBookPart.Workbook.Save();

Then when I try and open the Excel document it says the file is corrupted because the Relationship Id for the hyperlink cannot be found. How do you setup or create that Relationship Id?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:33:25+00:00Added an answer on May 15, 2026 at 12:33 am

    I was able to add a hyperlink to a cell using System.IO.Packaging code:

    private void HyperlinkCreate(PackagePart part, XmlNamespaceManager nsm, XmlNode _cellElement, string CellAddress)
    {
            Uri _hyperlink = new Uri("http://www.yahoo.com");
            XmlNode linkParent = _cellElement.OwnerDocument.SelectSingleNode("//d:hyperlinks", nsm);
            if (linkParent == null)
            {
                // create the hyperlinks node
                linkParent = _cellElement.OwnerDocument.CreateElement("hyperlinks", @"http://schemas.openxmlformats.org/spreadsheetml/2006/main");
                XmlNode prevNode = _cellElement.OwnerDocument.SelectSingleNode("//d:conditionalFormatting", nsm);
                if (prevNode == null)
                {
                    prevNode = _cellElement.OwnerDocument.SelectSingleNode("//d:mergeCells", nsm);
                    if (prevNode == null)
                    {
                        prevNode = _cellElement.OwnerDocument.SelectSingleNode("//d:sheetData", nsm);
                    }
                }
                _cellElement.OwnerDocument.DocumentElement.InsertAfter(linkParent, prevNode);
            }
            string searchString = string.Format("./d:hyperlink[@ref = '{0}']", CellAddress);
            XmlElement linkNode = (XmlElement)linkParent.SelectSingleNode(searchString, nsm);
            XmlAttribute attr;
            if (linkNode == null)
            {
                linkNode = _cellElement.OwnerDocument.CreateElement("hyperlink", @"http://schemas.openxmlformats.org/spreadsheetml/2006/main");
                // now add cell address attribute
                linkNode.SetAttribute("ref", CellAddress);
                linkParent.AppendChild(linkNode);
            }
    
            attr = (XmlAttribute)linkNode.Attributes.GetNamedItem("id", @"http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            if (attr == null)
            {
                attr = _cellElement.OwnerDocument.CreateAttribute("r", "id", @"http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                linkNode.Attributes.Append(attr);
            }                     
    
            PackageRelationship relationship = null;
            string relID = attr.Value;
            if (relID == "")
                relationship = part.CreateRelationship(_hyperlink, TargetMode.External, @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
            else
            {
                relationship = part.GetRelationship(relID);
                if (relationship.TargetUri != _hyperlink)
                    relationship = part.CreateRelationship(_hyperlink, TargetMode.External, @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
            }
            attr.Value = relationship.Id;
    }
    

    I then translated this code using the Open XML SDK 2.0 and it doesn’t work. It seems the AddHyperlinkRelationship method doesn’t actually add the relationship to the .rels file. I’m not sure why, but it sure seems like a bug to me.

    private void HyperlinkCreate(PackagePart part, XmlNamespaceManager nsm, XmlNode _cellElement, string CellAddress)
        {
    
    WorksheetPart workSheetPart = ExcelUtilities.GetWorkSheetPart(mWorkBookPart, "Program");
                Uri hyperlinkUri = new Uri("http://www.yahoo.com", UriKind.Absolute);
    
                Hyperlinks hyperlinks = workSheetPart.Worksheet.Descendants<Hyperlinks>().FirstOrDefault();
    
                // Check to see if the <x:hyperlinks> element exists; if not figure out 
                // where to insert it depending on which elements are present in the Worksheet
                if (hyperlinks == null)
                {
                    // Create the hyperlinks node
                    hyperlinks = new Hyperlinks();
    
                    OpenXmlCompositeElement prevElement = workSheetPart.Worksheet.Descendants<ConditionalFormatting>().FirstOrDefault();
                    if (prevElement == null)
                    {
                        prevElement = workSheetPart.Worksheet.Descendants<MergeCells>().FirstOrDefault();
                        if (prevElement == null)
                        {
                            // No FirstOrDefault needed since a Worksheet requires SheetData or the excel doc will be corrupt
                            prevElement = workSheetPart.Worksheet.Descendants<SheetData>().First();
                        }
                    }
                    workSheetPart.Worksheet.InsertAfter(hyperlinks, prevElement);
                }
                Hyperlink hyperlink = hyperlinks.Descendants<Hyperlink>().Where(r => r.Reference.Equals(CellAddress)).FirstOrDefault();
                if (hyperlink == null)
                {
                    hyperlink = new Hyperlink() { Reference = CellAddress, Id = string.Empty };
    
                }
    
                HyperlinkRelationship hyperlinkRelationship = null;
                string relId = hyperlink.Id;
                if (relId.Equals(string.Empty))
                {
                    hyperlinkRelationship = workSheetPart.AddHyperlinkRelationship(hyperlinkUri, true);
                }
                else
                {
                    hyperlinkRelationship = workSheetPart.GetReferenceRelationship(relId) as HyperlinkRelationship;
                    if (!hyperlinkRelationship.Uri.Equals(hyperlinkUri))
                    {
                        hyperlinkRelationship = workSheetPart.AddHyperlinkRelationship(hyperlinkUri, true);
                    }
                }
                hyperlink.Id = hyperlinkRelationship.Id;
                hyperlinks.AppendChild<Hyperlink>(hyperlink);
                workSheetPart.Worksheet.Save();     
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 397k
  • Answers 397k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The constructor for java.sql.Date takes a long (milliseconds since 1970)… May 15, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer It's an attribute you put on the <class> element when… May 15, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer If you create a thread, you have by default no… May 15, 2026 at 3:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.