Creating Excel file from dataset data [modified]
-
Hi, I have dataset and and a hyperlink.when somebody click on that link it should download the ms-excel file. I dont want to create it and store it on the web server. I would like to create it on the fly and send a temp file or something like that. Can anybody please help me? Thanks, NIKI -- modified at 0:23 Friday 30th June, 2006
-
Hi, I have dataset and and a hyperlink.when somebody click on that link it should download the ms-excel file. I dont want to create it and store it on the web server. I would like to create it on the fly and send a temp file or something like that. Can anybody please help me? Thanks, NIKI -- modified at 0:23 Friday 30th June, 2006
Hello Niki, You can create excel file on the fly. I have posted in my previous message that how to create excel file on the file and give to user for download. First of all you need to generate html code in table format from dataset.I am writing the code for that as well. ---------------------------------------------------------------------- //Write a table structure from dataset //Suppose your dataset name is ds then write following code on click event of hyperlink: string strHtml = ""; DataTable dt = ds.Tables(0); foreach(DataRow dr in dt.Rows) { strHtml+=""; for(int i=0;i"; } strHtml+=""; } strHtml+="
"; //Clear the Response Headers Response.Clear(); Response.Charset = String.Empty; // String.Empty(); //Set Content Type and add header Response.ContentType = "application/vnd.ms-excel"; //Add Header to create a file on the fly, give file name also. (i.e. abc.xls) Response.AddHeader("Content-Disposition", "attachment; filename=abc.xls;"); this.EnableViewState = false; //Declare new HtmlTextWriter System.IO.StringWriter myTextWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter myHtmlTextWriter = new System.Web.UI.HtmlTextWriter(myTextWriter); //Declare new HtmlGenericControl and feed it with the strHtml HtmlGenericControl gn = new HtmlGenericControl(); gn.InnerHtml=strHtml; //Finally render the HtmlGenericControl to Response gn.RenderControl( myHtmlTextWriter); Response.Write(myTextWriter.ToString()); Response.End(); ---------------------------------------------------------------------- Regards, Ketan. -- modified at 7:30 Wednesday 5th July, 2006
-
Hello Niki, You can create excel file on the fly. I have posted in my previous message that how to create excel file on the file and give to user for download. First of all you need to generate html code in table format from dataset.I am writing the code for that as well. ---------------------------------------------------------------------- //Write a table structure from dataset //Suppose your dataset name is ds then write following code on click event of hyperlink: string strHtml = ""; DataTable dt = ds.Tables(0); foreach(DataRow dr in dt.Rows) { strHtml+=""; for(int i=0;i"; } strHtml+=""; } strHtml+="
"; //Clear the Response Headers Response.Clear(); Response.Charset = String.Empty; // String.Empty(); //Set Content Type and add header Response.ContentType = "application/vnd.ms-excel"; //Add Header to create a file on the fly, give file name also. (i.e. abc.xls) Response.AddHeader("Content-Disposition", "attachment; filename=abc.xls;"); this.EnableViewState = false; //Declare new HtmlTextWriter System.IO.StringWriter myTextWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter myHtmlTextWriter = new System.Web.UI.HtmlTextWriter(myTextWriter); //Declare new HtmlGenericControl and feed it with the strHtml HtmlGenericControl gn = new HtmlGenericControl(); gn.InnerHtml=strHtml; //Finally render the HtmlGenericControl to Response gn.RenderControl( myHtmlTextWriter); Response.Write(myTextWriter.ToString()); Response.End(); ---------------------------------------------------------------------- Regards, Ketan. -- modified at 7:30 Wednesday 5th July, 2006
-
Hi Ketan, Thank You very much for the valuable help. As I know, we dont have onclick event for HyperLink. So, where should i place this code??? I really appreciate your help. Thanks, NIKI
Set NavigateUrl property of HyperLink control to current page name and add one flag in query string. For example, if your current page name is page.aspx then set NavigateUrl property as : "page.aspx?excel=true" And then, in page_load() event add following code: if(Request.QueryString["excel"]=="true") { //Write stuff to create excel file. } I am sure this trick will work. Regards, Ketan.
-
Set NavigateUrl property of HyperLink control to current page name and add one flag in query string. For example, if your current page name is page.aspx then set NavigateUrl property as : "page.aspx?excel=true" And then, in page_load() event add following code: if(Request.QueryString["excel"]=="true") { //Write stuff to create excel file. } I am sure this trick will work. Regards, Ketan.
Hi Ketan, Thank you very much sir. But I am working on Share Point server. And this functionality i have to add in my WebPart.I am facing hard time for this problem.If you can help me, it will be really great. Thank you very much ketan for your help. NIKI
-
Hi Ketan, Thank you very much sir. But I am working on Share Point server. And this functionality i have to add in my WebPart.I am facing hard time for this problem.If you can help me, it will be really great. Thank you very much ketan for your help. NIKI
Well, I dont know anything about Share Point server, I am sorry I cant help now anymore. :sigh: Regards, Ketan