Search code examples
c#excelepplus

Error in Excel with epplus (HYPERLINK) formula


I have a problem with epplus i.e. it generates an excel document with different types of data in one of the columns I have to put a link to some resource, I created a code that does it and puts it all into the formula, throwing it to Excel, but after generating Excel and opening it I get the Error:

We Found A Problem With Some Content In Excel

After opening it returns the error:

Removed Records: Formula from /xl/worksheets/sheet1.xml part

I no longer have any idea about this error, some good advice on how to fix / improve it?

My code:

                    foreach (var item in url)
                    {

                        if (url.First() == item)
                            cli.Add(String.Format(@"=HYPERLINK(""{0}"",""{1}"")&"" """, item.address, item.name));
                        else
                        if (url.Last() == item)
                            cli.Add(String.Format(@"&HYPERLINK(""{0}"",""{1}"")", item.address, item.name));
                        else
                            cli.Add(String.Format(@"&HYPERLINK(""{0}"",""{1}"")&""  """, item.address, item.name));
                    }
                    ws.Cells[row, 6].Style.WrapText = true;
                    ws.Cells[row, 6].Formula = String.Join("", cli);

Solution

  • I have created this and it works fine. I can click on the link and it opens just fine.. Try this, I hope this is what you are looking for. I have changed the string format ..

    using (ExcelPackage excel = new ExcelPackage())
            {
                excel.Workbook.Worksheets.Add("Worksheet1");
                excel.Workbook.Worksheets.Add("Worksheet2");
                excel.Workbook.Worksheets.Add("Worksheet3");
    
                var worksheet = excel.Workbook.Worksheets["Worksheet1"]; 
    
                string FileRootPath = "http://www.google.com";
    
                string url  = String.Format("HYPERLINK(\"{0}\",\"" + "{1}" + "\")", FileRootPath, "Test display");
    
                worksheet.Cells[1, 1].Formula = url; // "HYPERLINK(\"" + FileRootPath + "\",\"" + "TEST display" + "\")";
    
                FileInfo excelFile = new FileInfo(@"C:\ProjectWork\test.xlsx");
                excel.SaveAs(excelFile);
            }