Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将超链接添加到excel工作表closedxml_C#_Excel_Hyperlink_Closedxml - Fatal编程技术网

C# 将超链接添加到excel工作表closedxml

C# 将超链接添加到excel工作表closedxml,c#,excel,hyperlink,closedxml,C#,Excel,Hyperlink,Closedxml,我正在将一个数据表转换为excel,它运行良好。我在数据表中有4列。第1列和第2列是简单文本。第3列和第4列是超链接url和显示文本。所有4列都以excel格式导出,代码如下: string fileName = ConfigurationManager.AppSettings["filename"]; string sheetName = System.IO.Path.GetFileNameWithoutExtension(fileName)

我正在将一个数据表转换为excel,它运行良好。我在数据表中有4列。第1列和第2列是简单文本。第3列和第4列是超链接url和显示文本。所有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);
            }
        }
我想创建包含第3列(url)和第4列(显示文本)的超链接,该超链接将在导出的excel中显示为第3列


任何想法

如果我没有弄错的话,您的代码使用的是ClosedXML库,而不是OpenXMLSDK

在OpenXMLSDK中添加超链接有两种方法:

  • =HYPERLINK
    公式写入单元格
    Cell cell = new Cell()
    {
      CellReference = "A3",
      DataType = new EnumValue<CellValues>(CellValues.Number),
      CellFormula = "=HYPERLINK(yourlinklocationcellhere, yourfriendlyhyperlinknamehere)"
    };
    
    Cell Cell=new Cell()
    {
    CellReference=“A3”,
    数据类型=新的枚举值(CellValues.Number),
    CellFormula=“=超链接(yourlinklocationcellhere,yourfriendlyhyperlinknamehere)”
    };
    
  • 在工作表中创建超链接对象。

您必须在单元格中循环并手动添加超链接:

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

您可以在ClosedXML中找到更多关于超链接的信息,网址为

,进一步了解@Francois Botha的早期解决方案,它不需要太多代码

e、 g.在VB.NET中,我刚刚使用以下代码将包含URL的字符串列(已从SQLServer数据库查询的结果数据表填充)转换为可单击链接:

    For Each wsrow In wc.Rows
        wsrow.Cell("G").Hyperlink = New XLHyperlink(wsrow.Cell("G"))
    Next
更新:否,这似乎有效-突出显示的URL-但链接内容为空:( 非常有用。仅供参考,我使用的是相当旧的OpenXML版本,0.68.1。*


*昨天我尝试升级ClosedXML,但它破坏了一些基本的现有代码,图像显示功能对我来说不起作用-我时间太短,无法进一步处理它。

我不想在单元格中循环,因为这会耗费时间。如果您想使用ClosedXML,这是您唯一的选择。