Export Gridview Data to Word Document in ASP.NET

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">
<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>
<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.

<add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />

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.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;


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]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s