Export gridview to word and excel. ms word 2007 & excel 2007

Any one have the code for exporting a gridview to ms word 7.0 and excel 7.0
westdhAsked:
Who is Participating?
 
princeatapiCommented:
protected void Button1_Click(object sender, EventArgs e)
    {
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = String.Empty;
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.End();
    }
0
 
princeatapiCommented:
ive bounded the export with a button click event , you can relocate to any event you wish
0
 
princeatapiCommented:
fix the suitable MIME type for word export  n ths line Response.ContentType = "application/vnd.xls";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.