In this post, I will explain how to export Gridview data to Word document in ASP.NET
Coding Part:
1. Let’s add a Gridview control to aspx page and fill it with our sample Employees table from Northwind Database. Here is a code snippet.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True"> <Columns> <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" InsertVisible="False" SortExpression="EmployeeID"></asp:BoundField> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName"></asp:BoundField> <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName"></asp:BoundField> <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address"></asp:BoundField> <asp:BoundField DataField="City" HeaderText="City" SortExpression="City"></asp:BoundField> <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode"></asp:BoundField> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country"></asp:BoundField> </Columns> </asp:GridView> <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>' SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Address], [City], [PostalCode], [Country] FROM [Employees]"></asp:SqlDataSource>
2. Set the connection string in the web.config file as shown below.
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" /> </connectionStrings>
3. Now let’s write a code to export this Gridview data to word document. Since we need to use System IO operations, we need to include following namespace.
using System.IO;
4. Let’s add a asp button to aspx page, call it as “Export to Word Document” and on the “onClick” event of the button write following code.
Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=EmployeesData.doc"); Response.ContentType = "application/ms-word"; StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(htmlTextWriter); Response.Write(stringWriter.ToString()); Response.End();
To avoid the exception like “Control of type must be placed inside a form tag with runat=server.“, we need to add following method in the code behind.
public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }
5. Once done, rebuild the solution and run it. You will see following output on the page.
6. Now let’s click on “Export to Word Document” button, download the file and see the output in word document.
Source Code Download
Github [Repository link]
Box.com [Direct Link to Zip file]
Related articles
- Export Gridview to PDF in ASP.NET using iTextSharp Library (surajdeshpande.wordpress.com)
- Export Gridview Data to Excel in ASP.NET (surajdeshpande.wordpress.com)
- Export Gridview Data to CSV File in ASP.NET (surajdeshpande.wordpress.com)
3 comments