Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
below is the code how i export the page to excel file, but the image inside excel is not display after export.
the image is at server directory, /image/logo.jpg , after i download the excel(inside download folder), the image find the path from "C:\Users\user\Downloads\image\logo.jpg" , how can i include the image inside the excel file?
C#
StringWriter strWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=MyFile.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";


i also tried to export to pdf, but failed, mention that the pdf file is corrupted. any advice? code as below
C#
Response.AddHeader("content-disposition", "attachment;filename=MyFile.pdf");
Response.ContentType = "application/pdf";


UPDATE:
i tried few approaches, all return the same result.
the excel file display:
C#
<img src="http://localhost:49524/logo.jpg"/>
instead of the image file itself..
Posted
Updated 20-Feb-13 15:26pm
v4

 
Share this answer
 
Try this:
C#
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=filename.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
string tag = "<img src="imagepath & filename" />";
Response.Write(tag);
Response.End();


And also Google can give you lots of tips if you'll try that Export image To Excel in Asp.net[^].

--Amit
 
Share this answer
 
Comments
melvintcs 20-Feb-13 4:20am    
what is the usage of DataGrid.RenderControl. i only have images, label, and textbox, can i disable that code? that line give me error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900