connect sql datasource to excel in .net

Last post 07-06-2009 11:12 AM by Trini_NAtwaroo. 4 replies.

Sort Posts:

  • connect sql datasource to excel in .net

    07-04-2009, 9:35 PM

     hey guys , I need some assistance and advice.

    The final product is an excel sheet. I have a query which returns result ideally I would be doing this using an sql datasource in .net. However what i would accomplish is to "export" the results returned from the query to an excel sheet fill with the returns; as well as additionally add in a title etc. So I was thinking if its possible to connect the datasource to a excel sheet and if this is possible how exactly will I be doing this.

    Please help any ideas or advice is welcomed

    Thanks

  • Re: connect sql datasource to excel in .net

    07-05-2009, 11:30 PM
    • All-Star
      91,768 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs
    "Code,Beer and Music ~ my way of being a programmer"

  • Re: connect sql datasource to excel in .net

    07-06-2009, 9:15 AM

     hey thanks I actually used something similar as it and worked, however I am wondering if I can format the excel spreadsheet before it exports eg I want a title and subject of the excel before the gridview appears, do you have any idea by chance how to do this?

     

    Thanks

  • Re: connect sql datasource to excel in .net

    07-06-2009, 9:37 AM
    Answer
    • Star
      9,468 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,641

     I didn't have a change to try it, but a couple of days ago I found a very promising solution on CodePlex:

    ExcelPackage: Office Open XML Format file creation

  • Re: connect sql datasource to excel in .net

    07-06-2009, 11:12 AM
    hey thanks for all your help, i used the code below to add the text before exporting to excel.
    protected void Page_Load(object sender, EventArgs e)
            {
                VerifyRenderingInServerForm(form1);
                gvToExport.GridLines = GridLines.Both;
    
            }
            protected void btnExport_Click(object sender, EventArgs e)
            {
                Panel p = new Panel();
                Panel p2 = new Panel();
                Panel p3 = new Panel();
                Panel p4 = new Panel();
                Label lb = new Label();
                Label lb2 = new Label();
                Label lb3 = new Label();
                Label lb4 = new Label();
                lb.Text = "Company Name
                lb2.Text = "Title of report:";
                lb3.Text = "1st March to 31st March 2008"; // test
                lb4.Text = "------";
                p.Controls.Add(lb);
                p2.Controls.Add(lb2);
                p3.Controls.Add(lb3);
                p4.Controls.Add(lb4);
                    
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.xls";
                //StringWriter StringWriter = new System.IO.StringWriter();
               // HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
               // gvToExport.RenderControl(HtmlTextWriter);
               // Response.Write(StringWriter.ToString());
               // Response.End();
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite =
                new HtmlTextWriter(stringWrite);
                // turn off paging 
                gvToExport.AllowPaging = false;
                gvToExport.DataBind();
                p.RenderControl(htmlWrite);
                p2.RenderControl(htmlWrite);
                p3.RenderControl(htmlWrite);
                p4.RenderControl(htmlWrite);
                gvToExport.RenderControl(htmlWrite);          
                
                Response.Write(stringWrite.ToString());
                Response.End();
                // turn the paging on again 
                gvToExport.AllowPaging = true;
                gvToExport.DataBind();
    
            }
    
            public override void VerifyRenderingInServerForm(Control control)
            {
                /* Verifies that a Form control was rendered */
            }
    
            protected void gvToExport_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                gvToExport.PageIndex = e.NewPageIndex;
            }
    
    Thanks

     

     

Page 1 of 1 (5 items)