Exporting Image and GridView to Word/Excel is Impossible!

M

manmit.walia

Hello Fellow Developers,

I have a small problem that you might be able to help me with. The
method that I am trying to create is the ability to export a GridView
and a image to word or excel. I know that some people have already post
about this topic but could not find a reliable answer. To my knowledge,
I have the code down right. The GridView writes to the word document
perfectly just not the image. All I get is a red 'x' image notifying me
that word cannot find the path of the image.

Below is my code. Any help would be grateful.

Legend:

---------------------------------

partsGrid = GridView

pieImage = asp.net Image, which is dynamically created using GDI+

---------------------------------


intro = "<div align=center><font style=FONT-SIZE:'18pt'; COLOR:
'#8B0000'; FONT-FAMILY: 'Verdana'>" + rptlbltype.Text +
"</font></div><br>";
intro = intro + "<div align=center><font style=FONT-SIZE:'11pt'; COLOR:
'#8B0000'; FONT-FAMILY: 'Verdana'>Date From" + " " + sDate.Text + " " +
"to" + " " + eDate.Text + "</font></div><br>";

string temp = rptlbltype.Text + "-" + " " + sDate.Text + "to" +
eDate.Text;

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=" +
rptlbltype.Text + ".doc");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

StringWriter stringWrite2 = new StringWriter();

HtmlTextWriter htmlWrite2 = new HtmlTextWriter(stringWrite2);

partsGrid.RenderControl(htmlWrite);

pieImage.RenderControl(htmlWrite2);



string totalText = intro + stringWrite.ToString() + "<br/>" +
stringWrite2.ToString() ;

Response.Write(totalText);

Response.End();
 
S

Steve C. Orr [MVP, MCSD]

You need to make sure you're specifying the full path to the image, not a
relative path.
 
P

Pankaj.3107

Hi Everyone,
I do also have the same problem....
I have also given the full path of image,but still it is not showing
the image in Word document...
The GridView writes to the word document
but not the image. All it show is a red 'x' image . but on clicking
the image on word document it display the image....
any help would be gratefull..
thanks
Pankaj
 
Joined
May 21, 2010
Messages
1
Reaction score
0
Exporting image to excel

Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously

Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used

HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView2.GridLines = GridLines.Both;
GridView2.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top