Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why data of GridView are not displaying in ms word document


My cs code is given below
C#
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();
        Response.Clear();
        Response.Charset = "";
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
        Response.ContentType = "application/ms-word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
        gvdetails.AllowPaging = false;
        gvdetails.DataBind();
        dg.HeaderStyle.Font.Bold = true;
        dg.HeaderStyle.Font.Size = 10;
        dg.DataBind();
        dg.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
        
    }
Posted
Updated 1-Jan-12 23:42pm
v2
Comments
Janardan Pandey 2-Jan-12 5:48am    
only blank document is opening by this coading

1 solution

Try this,

Response.Clear();
HtmlForm form = new HtmlForm();
string attachment = "attachment;filename=Customers.doc";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-word";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gvdetails);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
 
Share this answer
 
Comments
Janardan Pandey 2-Jan-12 6:27am    
But What is the Name Space for HtmlForm form=new HtmlForm()?


Error is giving in this line for namespace for HtmlForm

Error line is given below

HtmlForm form=new HtmlForm();
Supriya Srivastav 2-Jan-12 6:32am    
import "System.Web.UI.HtmlControls"

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