Click here to Skip to main content
16,015,072 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi i wanted the code for transferring the grid view data to an excel file.on button click what action has to be performed?
Posted
Comments
Kenneth Haugland 12-Aug-12 13:05pm    
Use Office interop or OleDb connection....
sravsailu 12-Aug-12 13:09pm    
i want to know what should be written when a button say export is clicked
[no name] 12-Aug-12 13:13pm    
No one is going to write the code for you. That is your job to do not ours.
Kenneth Haugland 12-Aug-12 13:23pm    
Univote on that :)

Interop is easiest way I found dealing with excel through C#. You will find many articles on this[^].
 
Share this answer
 
Comments
Kenneth Haugland 14-Aug-12 1:14am    
Gives you more edit options (Format Styles Formula etc) also :)
Two choises:

Interop:
http://support.microsoft.com/kb/302084[^]

OleDb:
Reading and Writing Excel using OLEDB[^]

Other options could be found on Google.
 
Share this answer
 
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        this.EnableViewState = false;    //in case error occurs add this along with EnableEventValidation="false" set in source
        Response.Clear();

        Response.Buffer = true;

        Response.AddHeader("content-disposition", "attachment;filename=exp.xls");

        Response.Charset = "";

        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter ht = new HtmlTextWriter(sw);

        GridView1.AllowPaging = false;

        GridView1.DataBind();

        GridView1.RenderControl(ht);

        Response.Output.Write(sw.ToString());

        Response.Flush();

        Response.End();

    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {

    }
 
Share this answer
 
v2
Check this article.

I have explained how to export data from grid to excel with formatting.

Export DataTable to Excel with Formatting in C#[^]
 
Share this answer
 

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