• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Export Image from .NET to Excel using C#

I use this code

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        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
        Response.End();
    }
    public string HTML()
    {
        Page page1 = new Page();
        HtmlForm form1 = new HtmlForm();

        page1.EnableViewState = false;

        page1.Controls.Add(form1);
        form1.Controls.Add(GVJugosVinos);
        form1.Controls.Add(GVMarcasPropias);
        form1.Controls.Add(GVSidra);
        form1.Controls.Add(GVDescuentos);
        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.DesignerInitialize();
        page1.RenderControl(writer2);
        writer2.Write("\n</body>\n</html>");
        page1.Dispose();
        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
0
tenriquez199
Asked:
tenriquez199
  • 2
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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:
http://www.mywebserver.com/images/ValleRedondo.jpg

Example:
writer2.Write(@"<img src=""http://www.mywebserver.com/images/ValleRedondo.jpg"" />");

Open in new window

0
 
tenriquez199Author Commented:
Thanks
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Un placer haberte ayudado! ;)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now