Export Image from .NET to Excel using C#

Posted on 2011-05-06
Last Modified: 2013-12-16
I use this code

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        Response.Buffer = true;
        Response.ContentType = "application/";
        Response.AddHeader("Content-Disposition", "attachment;filename=ResumenNacional" + ndia + "" + LMes.Text + "" + anio + ".xls");
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.Default;
        Response.Write(HTML()); //Llamada al procedimiento HTML
    public string HTML()
        Page page1 = new Page();
        HtmlForm form1 = new HtmlForm();

        page1.EnableViewState = false;

        System.Text.StringBuilder builder1 = new System.Text.StringBuilder();
        System.IO.StringWriter writer1 = new System.IO.StringWriter(builder1);
        HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
        writer2.Write("<img src= ~/images/ValleRedondo.jpg >");

        writer2.Write("<table><tr><td><font face=Arial size=5><center>Resumen Nacional</center></font></td></tr></table><br>");

        writer2.Write("<table>\n<tr>\n<td class=TD width=35%><b>Fecha  :</b></td><td width=65% align=left>" + ndia + "/" + mes + "/" + anio + "</td>\n</tr>\n<tr>\n<td class=TD><b>Tipo de Venta:</b></td><td>" + SeleccionTipo.SelectedItem.ToString().Trim() + "</td>\n</tr>\n<tr>\n<td class=TD><b>Mes:</b></td><td>" + LMes.Text.Trim() + "</td>\n</tr>\n</table>\n<br><br>");

        page1 = null;

        return builder1.ToString();

but when i open the file in excel dont show the image,

I have my Image in to a directory of my application

also if anyone can help me to export gridview to pdf.
try and several libraries but I get blank pdf
Question by:tenriquez199
    LVL 17

    Accepted Solution

    Hi, check this line:
    writer2.Write("<img src= ~/images/ValleRedondo.jpg >");

    Open in new window

    I think that you must change it to an absolute url, something like:

    writer2.Write(@"<img src="""" />");

    Open in new window

    LVL 1

    Author Closing Comment

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Un placer haberte ayudado! ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now