Epplus read hyperlink with html fragment i

2019-07-17 06:44发布

I got Excel xlsx document with hyperlinks. Hyperlinks have adresses and subaddresses (that's the way VBA call Html fragments, all after # sign)

Epplus library has Hyperlink property for every cell, but it has only first part of html address, so instead of

stackoverflow.com#footer

I got:

stackoverflow.com

Is there any way to read the html fragment part with this library ?

Code for reading hyperlinks via epplus:

        FileInfo xlsxFile = new FileInfo(_filePath);
        using (ExcelPackage pck = new ExcelPackage(xlsxFile))
        {
            var wb = pck.Workbook;
            if (wb == null)
                return null;

            var ws = wb.Worksheets.FirstOrDefault();
            ExcelRange er = ws.Cells[0,0];
            var hyperlink = er.Hyperlink;

1条回答
等我变得足够好
2楼-- · 2019-07-17 07:15

It seems to be an issue with the way excel store hyperlinks and the way Epplus reads them. Excel stores the hyperlinks both in the worksheet itself as well as the relationship file for the worksheet which is a file that stores any kind of cross referencing between workbook parts (worksheets, styles, strings, etc). This all has to do with the structure of the an xlsx file which is xml based off of the OpenOffice XML standard: OpenOffice XML Info

So the problem is Epplus is relying on that relationship file which does not contain the fragment while the `hyperlink' node in the worksheet xml does. You can see all of this in its gory detail if you open up the xlsx file as a zip file by renaming it.

So, the short answer is you are forced to use the `.Value' of the cell object. Not as clean but it will work. For example, if I create a cell like this:

Excel with URL+Fragment

with this code:

var fi = new FileInfo(@"c:\temp\Html_Fragment.xlsx");
using (var pck = new ExcelPackage(fi))
{
    var wb = pck.Workbook;
    var ws = wb.Worksheets.FirstOrDefault();
    ExcelRange er = ws.Cells[1,1];
    var hyperlink = er.Hyperlink;

    Console.WriteLine(er.Value);
    Console.WriteLine("{{Value: {0}, Hyperlink: {1}}}", er.Value, er.Hyperlink.AbsoluteUri);
}

Gives this:

{
 Value: https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx#Anchor_3, 
 Hyperlink: https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx
}
查看更多
登录 后发表回答