Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Creating Excel file from dataset data [modified]

Creating Excel file from dataset data [modified]

Scheduled Pinned Locked Moved ASP.NET
sysadminhelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dhruvil
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • D Dhruvil

      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

      K Offline
      K Offline
      ketankumar
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • K ketankumar

        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

        D Offline
        D Offline
        Dhruvil
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • D Dhruvil

          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

          K Offline
          K Offline
          ketankumar
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • K ketankumar

            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.

            D Offline
            D Offline
            Dhruvil
            wrote on last edited by
            #5

            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

            K 1 Reply Last reply
            0
            • D Dhruvil

              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

              K Offline
              K Offline
              ketankumar
              wrote on last edited by
              #6

              Well, I dont know anything about Share Point server, I am sorry I cant help now anymore. :sigh: Regards, Ketan

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups