Hi,
I had a gridview with images,while i export the grid view to excel all the images are embeded into excel in the open mode.
if i try to save the exported data the images are missing.Please help me how to achieve this.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 21, 2009, 1:54 AM EDT
Export the html page With images into excel:
string GridName="Demo"
HttpContext.Current.Response.ContentType ="application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & GridName & ".xls");
HttpContext.Current.Response.Charset = "";
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw =new System.Web.UI.HtmlTextWriter(tw);
Page.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
Don't forget Mark the answer
R6 reputationPosted Oct 21, 2009, 9:58 PM EDT
Thanks for the reply.
My idea is to upload a grid with images into excel,so i can achieve this by just rendering the Grid into Streamwriter.By this approch images are getting referenced by their url's, so if user opens the exported excel it is fine and if he saves it also works untill the connectivity between the user machine and the server is avail.
Thanks
Rakesh
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 21, 2009, 10:57 PM EDT
Master BillaRank #2,229 · 630 reputationPosted Oct 22, 2009, 12:31 AM EDT
use this code to this, just copy and paste to ur button click.
private void Excel_Export()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
thank you