简体   繁体   English

添加超链接到 excel sheet closedxml

[英]Add hyperlink to excel sheet closedxml

I am converting a data table to excel which is working fine.我正在将数据表转换为工作正常的 excel。 I have 4 columns in data table.我在数据表中有 4 列。 Column1 and Column2 are simple text. Column1 和 Column2 是简单的文本。 Column3 and Column4 are hyperlink url and display text. Column3 和 Column4 是超链接 url 并显示文本。 all 4 columns are exported as it is in excel with below code:所有 4 列都按 excel 中的原样导出,代码如下:

        string fileName = ConfigurationManager.AppSettings["filename"];
        string sheetName = System.IO.Path.GetFileNameWithoutExtension(fileName);
        using (XLWorkbook wb = new XLWorkbook())
        {               
            var ws = wb.Worksheets.Add(ptDataTable, sheetName);

            ws.Style.Font.FontName = "Arial";
            ws.Style.Font.FontSize = 10;
            ws.Style.Alignment.WrapText = true;
            ws.FirstRow().Style.Alignment.SetWrapText(true);

            ws.Style.Alignment.SetWrapText(true);
            ws.Style.Alignment.SetShrinkToFit(true);
            ws.Style.Alignment.Vertical = XLAlignmentVerticalValues.Top;
            ws.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
            ws.Rows().AdjustToContents();
            ws.Columns().AdjustToContents();

            ws.Rows(2,200).Style.Fill.SetBackgroundColor((XLColor.Transparent));
            ws.Columns(9, 10).Width = 50.0;
            ws.Range("A2:J200").Style.Border.SetInsideBorder(XLBorderStyleValues.Thin);
            ws.Range("A2:J200").Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin);


            using (MemoryStream stream = new MemoryStream())
            {
                wb.SaveAs(stream);
                return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
        }

I want to create hyperlink with column3(url) and column4(display text) and this hyperlink will be 3 column in exported excel.我想用 column3(url) 和 column4(display text) 创建超链接,这个超链接将是导出的 excel 中的 3 列。

Any idea!任何想法!

You will have to loop through the cells and add the hyperlinks manually:您将不得不遍历单元格并手动添加超链接:

ws.Cell(rowNumber, columnNumber).Hyperlink = new XLHyperlink(@"http://www.yahoo.com");

You can find more info about hyperlinks in ClosedXML at https://github.com/ClosedXML/ClosedXML/wiki/Using-Hyperlinks您可以在https://github.com/ClosedXML/ClosedXML/wiki/Using-Hyperlinks中找到有关 ClosedXML 中超链接的更多信息

If I am not mistaken, your code is using the ClosedXML library, not the OpenXMLSDK.如果我没记错的话,您的代码使用的是 ClosedXML 库,而不是 OpenXMLSDK。

There are two ways to add hyperlinks in OpenXMLSDK:在 OpenXMLSDK 中添加超链接有两种方法:

Further to @Francois Botha's earlier solution, it need not take much code.除了@Francois Botha 的早期解决方案,它不需要太多代码。

eg in VB.NET, I just used the following code to convert a string column containing URLs(already populated from my SQLServer database query's resultant DataTable) to clickable links:例如在 VB.NET 中,我只是使用以下代码将包含 URL 的字符串列(已经从我的 SQLServer 数据库查询的结果 DataTable 填充)转换为可点击的链接:

    For Each wsrow In wc.Rows
        wsrow.Cell("G").Hyperlink = New XLHyperlink(wsrow.Cell("G"))
    Next

UPDATE: No, that appeared to work - highlighted URL - but the link content is empty:( Works a treat. FYI I am using quite an old version of OpenXML, 0.68.1. * *

* I tried upgrading ClosedXML yesterday but it broke some essential existing code and the image display capability did not work for me - I was too short of time to mess about with it further. *我昨天尝试升级 ClosedXML,但它破坏了一些重要的现有代码,并且图像显示功能对我不起作用 - 我的时间太短,无法进一步处理它。

Francois Botha 's answer works, but it is pretty slow for a large number of hyperlinks (in the thousands, as each hyperlink generation takes up a couple of milliseconds to generate). Francois Botha的回答有效,但是对于大量超链接(数千个,因为每个超链接生成需要几毫秒来生成),它的速度非常慢。

A good alternative is to generate the hyperlink using a formula:一个不错的选择是使用公式生成超链接:

cell.FormulaA1 = $"=HYPERLINK(\"{url}\", \"{text}\")";

A function using this approach that also ensures valid hyperlink values can be found onCodidact .使用这种方法的 function 还确保可以在Codidact上找到有效的超链接值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM