Export GridView To Word

To retrieve the records from a table fom within a database is perfectly easy via the gridview server control which can be bound via some queries specific to the requirements.

However what ought to be done when we have to export the data into a Word/Excel/PDF/CSV format ?

Does it seem tough. May be but here in we go with the simple though tricky codes :

protected void btnExportWord_Click(object sender, EventArgs e)

{

Response.Clear();

Response.Buffer = true;

Response.AddHeader("content-disposition",

"attachment;filename=GridViewExport.doc");

Response.Charset = "";

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

StringWriter sw= new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;

GridView1.DataBind();

GridView1.RenderControl(hw);

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

Response.Flush();

Response.End();

}

Post a Comment

Previous Post Next Post