Epplus read hyperlink with html fragment i

北城余情 提交于 2020-01-04 06:34:13

问题


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:


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:

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
}


来源:https://stackoverflow.com/questions/32970864/epplus-read-hyperlink-with-html-fragment-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!