Export page to excel with logo image

13,062

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.

Share:
13,062
MelloG
Author by

MelloG

Updated on June 04, 2022

Comments

  • MelloG
    MelloG almost 2 years

    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();
    
  • MelloG
    MelloG about 13 years
    I think I know what you mean but how do I get the image (there is only one logo, there are no images on the grid) to a string? The rest of it I think I understood. Thanks man
  • MelloG
    MelloG about 13 years
    Look, this is what I have so far, but now I got to icons of those that says that the image cannot be displayed, on the excel file. 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());
  • Pravin Kumar
    Pravin Kumar almost 10 years
    Thanks..It helped me..@Arun Singh