How to Export GridView Control to Microsoft Word document(.doc) in ASP.Net ?
This little code snippet will help us to export the GridView content to Microsoft word document in ASP.Net.
CodeBehind protected void Page_Load(object sender, EventArgs e) { gvEmployee.DataSource = GetData(); gvEmployee.DataBind();
} protected void btnExpWord_Click(object sender, EventArgs e) { string attachment = "attachment; filename=Employee.doc"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/vnd.ms-word"; StringWriter stw = new StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw); gvEmployee.RenderControl(htextw); Response.Write(stw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) {
}
ASPX <asp:GridView ID="gvEmployee" runat="server"> </asp:GridView> <asp:Button ID="btnExpWord" runat="server" Text="Export to word" onclick="btnExpWord_Click" />
Read the below article to export GridView to Excel and PDF,
Export to PDF file in ASP.Net-Gridview to PDF, ASPX Page Content to PDF
Export to Excel in ASP.Net 2.0 - Gridview to Excel, DataTable to Excel
Happy Coding!!
|