简体   繁体   中英

how to create hyperlink programmatically in asp.net c# mvc 5 in excel sheet cell while export to excel using gridview

I want to create an hyperlink to a cell in Excel sheet while I am doing export to cell in C# MVC 5, I am using grid view to export to Excel. I have used anchor tag to create link, when the files gets exported it read that field as string. What is the other way to achieve this?

Below is what I have tried:

var result = from cssd in csdvm.FillControlStatusDetails.AsEnumerable()
                     select new
                     { 
            link = "<a href='www.google.com'> Link</a>
         }

GridView gvExport = new GridView();
gvExport.AutoGenerateColumns = false;

BoundField nameColumn = new BoundField();
nameColumn.DataField = "link";
nameColumn.HeaderText = "Link";
gvExport.Columns.Add(nameColumn);

gvExport.DataSource = result.AsQueryable().ToList();

gvExport.DataBind();
gen.CreateExportHeaderRow(gvExport, "Control Status - Details");

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ControlStatusDeatils-" + DateTime.Now.ToFileTimeUtc() + ".xls");
Response.ContentType = "application/vnd.xls";
Response.Charset = "";

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

gvExport.RenderControl(htw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

Thank you i found the way out..i used a for loop to add the link below is what is did

 for (int i = 0; i < gvExport.Rows.Count; i++)
var link = "<a href='" + gvExport.Rows[i].Cells[9].Text + "'>" + gvExport.Rows[i].Cells[10].Text+ "</a>" + "<br/>";
}

gvExport.Rows[i].Cells[9].Text = MvcHtmlString.Create(link).ToHtmlString();

using above code i am able to give an hyper link to an Excel cell while i am doing export to excel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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