Export GridView In Word using asp.net code


Print GridView using asp.net code code will export gridview in word file using this code you can call this function for export gridview directly in Ms word File

//Export Gridview on Button1 click event


protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");

Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.word";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridViewName.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{

}


Related Articles

More articles: Gridview export to word Gridview export

Comments

Author: Kirti Kaushik30 Apr 2010 Member Level: Bronze   Points : 1

When i used this code it is giving me the error - "Control 'ctl00_ContentPlaceHolder1_GVCatg' of type 'GridView' must be placed inside a form tag with runat=server".

Why this error comes? i m not able to correct it..

Author: Devendra Singh Rathore30 Apr 2010 Member Level: Silver   Points : 2

ok kirti

If You have any server Control in Your GridView then it will give You this error

for that
You have to change Enable Event Validation in your aspx page

EnableEventValidation="false"

Like



<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>






Guest Author: yobu08 Apr 2013

thanks a lot for this...



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: