0

I am using ClosedXML to generate excel from c# code.I need hyperlink on one particular column with multiple rows and every link is unique.

for (int i = 2; i <= result.Count + 1; i++)
{
    ws.Cell(i, 44).Value = "Download";
    url = folderPath + "type=testPhoto&userName=" + Convert.ToString(result[i - 2].testCode);
    ws.Cell(i, 44).Hyperlink = new XLHyperlink(url, "Download");
    ws.Cell(i, 44).Style.Font.FontColor = XLColor.AirForceBlue;
    ws.Cell(i, 44).Style.Font.Underline = XLFontUnderlineValues.Single;
}

But this loop is taking too much time. Is there is any alternative to skip this loop.

Suji
  • 1,326
  • 1
  • 12
  • 27
DSM
  • 46
  • 1
  • 4
  • How big is `result.Count`? You could separate the styling into one call like `ws.Column(44).Style.Font...`, but not sure how much difference that will make. – Raidri Jun 22 '16 at 08:35
  • result.Count can have maximum 1 lakh value. But in 15k only it is taking too much time. – DSM Jun 22 '16 at 08:39

1 Answers1

0

Try this optimized code and see if it is fast enough for you:

ws.Column(44).Style.Font.SetFontColor(XLColor.AirForceBlue)
    .Font.SetUnderline(XLFontUnderlineValues.Single;
url = folderPath + "type=testPhoto&userName="; 

for (int i = 2; i <= result.Count + 1; i++)
{
    ws.Cell(i, 44).SetValue("Download").Hyperlink =
        new XLHyperlink(url + Convert.ToString(result[i - 2].testCode), "Download");
}
Raidri
  • 17,258
  • 9
  • 62
  • 65