MelloG
MelloG

Reputation: 1064

Export page to excel with logo image

I have to export some data from an asp.net page to excel, so I use basically a table to create the custom headers I need then a GridView. All the data displays correctly when exported to excel, but when I added an Logo image to the html, it doesn't show up on the Excel file when exported.

Since I need to export it to Excel 2007 or later, I know I can use the Open XML stuff from Microsoft to export the data, the problem is that I already have everything done on the code and I wanted to know if there is another way to do that instead of doing all over again using Open XML.

If there isn't a way to do that without using Open XML, can anyone show me how I could export the data to it? I tried once but I didn't have much success. =/

BTW, I'm using C#.

Thanks in advance!

I've updated the code and now it looks like this, but I still can't see the image... =/

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    frmPlanoAcao.RenderControl(htmlWrite);

    StringWriter w = new StringWriter();
    HtmlTextWriter t = new HtmlTextWriter(w);
    imgLogo.RenderControl(t);
    var byte_array = System.Text.Encoding.ASCII.GetBytes(w.ToString());
    Response.Write(stringWrite.ToString());

    this.EnableViewState = false;

    Response.Clear();
    Response.Buffer = false;
    Response.Charset = "ISO-8859-1";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "plano_acao.xls"));
    Response.ContentType = "application/vnd.ms-excel";
    Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    Response.Write(getExcelStyling());
    Response.OutputStream.Write(byte_array, 0, byte_array.Length);
    Response.Write(stringWrite.ToString());
    Response.Write("</body>");
    Response.Write("</html>");
    Response.End();

Upvotes: 4

Views: 13739

Answers (3)

Arun Singh
Arun Singh

Reputation: 1546

Try the following code. I have tested at local IIS and it is working properly. Including the image like Header Image/Logo on top of the grid data.

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

you can customize your image's height and width as per your requirement. Same height and width setting will be required for the <TD> tag.

Upvotes: 2

Gilad
Gilad

Reputation: 2886

If you are using C# and want to export to Excel I would recommend EPPLUS

http://epplus.codeplex.com/

Will save you a whole lot of troubles now and in the future.

Upvotes: 0

iTSrAVIE
iTSrAVIE

Reputation: 834

You have to get images in StringWriter and use System.Text.Encoding.ASCII.GetBytes(stringwriter string) in array of bytes. then write these as outputStream.Write(byte_array, 0, byte_array.Length); where outputstream is HttpContext Response outputstream.

If you say, to convert Grid, Images, everything in one go to Excel, i have not tried. But, i can say, you can individually have both of them into Excel by their way.

Upvotes: 0

Related Questions