2

I'm using C# and DocumentFormat.OpenXml to parse an Excel File. This File has many cells with hyperlinks to external file.

How could I get addresses of those hyperlinks?

I have already tried with this: https://www.programmersought.com/article/22902085148/ but

var hyperlinks = worksheetPart.RootElement.Descendants<Hyperlinks>()

raises an error.

Thanks

2 Answers 2

4

This will do it for you. Change the foreach loop to whatever you want it to do. Right now it writes the URL to console:

using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(filepath, false))
{
    List<HyperlinkRelationship> hyperlinks = spreadsheet
        .GetAllParts()
        .SelectMany(p => p.HyperlinkRelationships)
        .ToList();

    foreach (HyperlinkRelationship hyperlink in hyperlinks)
    {
        string hyperlink_address = hyperlink.Uri.ToString();
        Console.WriteLine(hyperlink_address);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The link you provided actually had the correct solution, it wasn't well formatted in my oppinion though. A little tinkering got me the solution.

This is a "quick and dirty" hack thet will get you the hyperlink behind a cell. For easier comprehension i removed all error handling. A few try/catch blocks and null checks might be advisable in there.... ;)

public static string GetFullHyperlinkValue(SheetData sd, WorksheetPart wsp, string colIdx, int rowIdx)
    {
        // get all hyperlinks - you might want to store them ...
        var hyperlinks = wsp.RootElement.Descendants<Hyperlinks>().First().Cast<Hyperlink>();

        // get the cell at the address
        Cell cell = sd.Descendants<Row>().FirstOrDefault(p => p.RowIndex != null && p.RowIndex == rowIdx).Descendants<Cell>().FirstOrDefault(p => p.CellReference != null && p.CellReference == $"{colIdx}{rowIdx}");

        // get the Hyperlink object "behind" the cell
        Hyperlink hyperlink = hyperlinks.SingleOrDefault(i => i.Reference.Value == cell.CellReference.Value);

        // if the hyperlink has an anchor, the anchor will be stored without the # in hyperlink.Location
        string location = hyperlink.Location;

        // the URI is stored in the HyperlinkRelationship
        HyperlinkRelationship hyperlinkRelationship = wsp.HyperlinkRelationships.SingleOrDefault(i => i.Id == hyperlink.Id);
        var url = hyperlinkRelationship.Uri.ToString();

        return string.IsNullOrWhiteSpace(location) ? url.ToString() : $"{url}#{location}";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.