Pages

Monday 5 May 2014

write image into Excel cell

Following code is used to write image into Excel cell without any external
dll's.

To get absolute path of an image VirtualPathUtility.ToAbsolute()
function is used. This function works on local machine as well as
on IIS.

Before that I checked whether code running is on local machine or it is
deployed on IIS by using the function ResolveServerUrl(). This
function takes two arguments first one is image path using the function
VirtualPathUtility.ToAbsolute().

So ResolveServerUrl() is used check whether program is running on 
local(localhost) machine or whether it is running on Server(http) .


Code:
   HttpContext.Response.Write("");
  
string filePath=ResolveServerUrl(
       VirtualPathUtility.ToAbsolute("~/Content/Images/img1.jpg"),
       false);

 string imagepath = string.Format("<img src='{0}'/>", filePath);
                       
 HttpContext.Response.Write("");
 HttpContext.Response.Write(imagepath);
 HttpContext.Response.Write("");
 HttpContext.Response.Write("");

Function for checking:
public static string ResolveServerUrl(string serverUrl, bool forceHttps)
        {
            if (serverUrl.IndexOf("://") > -1)
                return serverUrl;

string newUrl = serverUrl;
Uri originalUri = System.Web.HttpContext.Current.Request.Url;
newUrl = (forceHttps ? "https" : originalUri.Scheme) +
                "://" + originalUri.Authority + newUrl;
 return newUrl;
        } 

No comments:

Post a Comment