Pages

Monday 28 April 2014

Add image into excel sheet without any dll

Writing image in excel default location is top left corner. To work the same code after deployment on IIS server don't forget to change the Image Url.
Code:
    HttpContext.Response.Clear();
    HttpContext.Response.ClearContent();
    HttpContext.Response.ClearHeaders();
    HttpContext.Response.Buffer = true;
    HttpContext.Response.ContentType = "application/ms-excel";
   
    HttpContext.Response.Write(@"<!DOCTYPE HTML PUBLIC "
             "-//W3C//DTD HTML 4.0 Transitional//EN"">");

  //Set the name of report as per query fired by the user.
 HttpContext.Response.AddHeader("Content-Disposition",
                  "attachment;filename=" + fileName);

HttpContext.Response.Charset = "utf-8";
HttpContext.Response.ContentEncoding = 
System.Text.Encoding.GetEncoding("windows-1250");

 HttpContext.Response.Write("<TR style='font-size:12.0pt;
 text-align:center; font-style:italic; text-decoration:underline;
 font-weight:bold;font-family:Book Antiqua;'>");

 string s = Server.MapPath("~\\Content\\Images\\abcd.jpg");
                     
   string filePath =
   System.Configuration.ConfigurationManager.AppSettings["LogoPath"];
   string imagepath = string.Format("<img src='{0}'/>", s);
                                        
 HttpContext.Response.Write("<Td>");
 HttpContext.Response.Write(imagepath);
 HttpContext.Response.Write("</Td>");
 HttpContext.Response.Write("</TR>");

No comments:

Post a Comment