Click here to Skip to main content
15,896,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Stored Procedure

SQL
Create procedure selectdata
as 
begin
    select * from Employee
end


In my front End I Created a Button in default.aspx page
HTML
<asp:Button id="btnsave" runat="server" Text="Send to Excel" onclick="btnsave_Click"/>

<asp:GridView id="gv" runat="server" >
</asp:GridView>


In default.aspx.cs
C#
protected void btnsave_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString="User id=aa; password=123; Initial Catalog=abc;Integrated Security=xxxx";
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "selectdata";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gv.DataSource = ds;
    gv.DataBind();
}


Output: It Displays the Data in Grid View

but is it possible to store the Data directly in Excel file in my PC
Posted
v2

 
Share this answer
 
Comments
Venkat Kumar chigulla 3-Sep-13 3:53am    
i got this Error
The type or namespace name does not exist in the namespace (are you missing an assembly reference?
Which link you referred? The error is quite clear; you are missing some assembly reference.
Add them.
Venkat Kumar chigulla 3-Sep-13 4:06am    
I followed the first link DataSet to Excel in Two steps – with Different Styles[^]

I added a reference "Microsoft.Office.Interop.Excel"

Even though i got the Same error
Do you have Excel installed on your machine?
Venkat Kumar chigulla 3-Sep-13 6:04am    
Excel 2010 is there in My PC and my Framework is 3.5
In Default.aspx
<asp:gridview id="gv" runat="server" >
    
</asp:gridview>
    
    
<asp:button id="btnsend" runat="server" text="Export to Excel" onclick="btnsend_Click"  />


In Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack != true)
        {
            BindGrid();
            
        }
    }

    private void BindGrid()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=aa; User Id=aa; Password=aa; Initial Catalog=aa; Integrated Security=xxxx";

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        cmd.CommandText = "select * from student_info";
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        DataTable dt = new DataTable();
        da.Fill(dt);

        gv.DataSource = dt;
        gv.DataBind();
    }

    protected void btnsend_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=abc.doc");
        Response.ContentType = "application/vnd.ms-word";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw); 
        BindGrid();
        gv.RenderControl(hw);
        Response.Output.Write(sw.ToString()); 
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900