Export page to excel with logo image

0 votes

I must export certain data from an asp.net website to excel, so I essentially use a table to make the special headers I require, followed by a GridView. When the data is exported to Excel, everything appears as intended, but when I added a Logo image to the HTML, the picture doesn't appear on the Excel file.

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 it all over again using Open XML 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, but the problem is that I already have everything done on the code.

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#.

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();
Oct 17 in Others by Kithuzzz
• 20,660 points
95 views

1 answer to this question.

0 votes

Try this:

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();
answered Oct 18 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer
0 votes
0 answers

Replacing H1 text with a logo image: best method for SEO and accessibility?

i want to link my logo to ...READ MORE

Feb 14 in Others by Kichu
• 19,040 points
1,899 views
0 votes
1 answer

datatable remove column on export to pdf and excel

When we are using jquery datatable for ...READ MORE

answered Feb 17 in Others by gaurav
• 22,040 points
1,853 views
0 votes
0 answers

Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

Feb 17 in Others by Edureka
• 13,640 points
106 views
0 votes
1 answer

Convert HTML to an image in Python

webkit2png. The original version is OSX-only, but luckily ...READ MORE

answered Feb 5, 2019 in Python by SDeb
• 13,300 points
15,115 views
0 votes
1 answer

Render HTML to an image

To answer your question, there are a ...READ MORE

answered Feb 8 in Java by Soham
• 9,670 points
825 views
0 votes
1 answer

Background images and SEO Info on it

Use alt text on a transparent image and ...READ MORE

answered Feb 21 in Others by narikkadan
• 37,660 points
114 views
0 votes
1 answer

seo - images and h1

SEO is speculative at best. Generally, the accepted ...READ MORE

answered Mar 11 in Digital Marketing by narikkadan
• 37,660 points
148 views
0 votes
1 answer

How to export data with functions to another workbook in excel

The issue appears to be that once ...READ MORE

answered Oct 30 in Others by narikkadan
• 37,660 points
33 views
0 votes
1 answer

Replacing H1 text with a logo image: best method for SEO and accessibility?

<h1> <a href="http://stackoverflow.com"> ...READ MORE

answered Feb 21 in Others by narikkadan
• 37,660 points
408 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP