How to Export GridView Data in Excel, Word & PDF format using Asp.Net

Here I will explain you how to export data from GridView in excel, word, and PDF format using Asp.Net.

Exporting to word & excel can be easily achieved using Asp.Net without any third party tools, but for exporting gridview to PDF, I am using iTextSharp which is a free library for exporting html to PDF.

To start with I have a GridView, in which I am showing Users details from my database for an example, and thre image buttons for exporting gridview to word, excel and PDF.
The HTML markup of the GridView and image buttons is as shown below

In above code we are using SqlDataSource for bind data in GridView from SQL. I have already explain this in my previous post. For more details click here!
Now if you run application gridview appears like this

Now, in code behind add the following namespaces.

  using System.IO;
  using System.Drawing;
  using iTextSharp.text;
  using iTextSharp.text.pdf;
  using iTextSharp.text.html.simpleparser;

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

The System.Drawing parent namespace contains types that support basic GDI+ graphics functionality. Child namespaces support advanced two-dimensional and vector graphics functionality, advanced imaging functionality, and print-related and typographical services. A child namespace also contains types that extend design-time user-interface logic and drawing.

The iTextSharp namespace allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.

Place the following code in code behind.

The BtnPDF_Click event renders the Gridview contents as Microsoft word format. You can see, I have use gvUsers.AllowPaging = false; to disabled paging before exporting, so that all the pages exported.

For exporting to excel format, you can do directly as done in case of word. But if you directly , the row background color is applied to all the columns in excel sheet. So in order to avoid it, I am changing the background color of each row to white. Then I am applying the background color to individual header cells. Thus when you export, formatting is applied only to the GridView cells and not to all.

For exporting to PDF, include iTextSharp dll to you application and library reference in code behind. By default the iTextSharp library does not support background colorof table cells and rows. Hence when you render it as PDF, GridView is rendered without any formatting. To exports the HTML with background color read tan artcle on

Now, if  you're running your application, you get an server error like control must be placed in inside of form tag”.

This error occurs when we are trying to export Gridview. This is happening because, compiler thinks that the control is not added to the form. Controls that are required to be inside <form runat=server> tags can call this method before they render so that an error message is shown if they are placed outside the tags.
To resolve this error, place the below code in code behind.

   public override void VerifyRenderingInServerForm(Control control)
        /* Verifies that the control is rendered */

Now , code runs properly for export to excel but if you're trying to export to PDF. You getting an another error like "RegisterForEventValidation can only be called during Render()". This error occurs, when we are trying to render control to response.When the EnableEventValidation property is set to true, ASP.NET validates that a control event originated from the user interface that was rendered by that control.

To resolve this error, set EnableEventValidation property false in your .aspx page like

<%@ Page EnableEventValidation="false" %>

Now , code runs successfully.....I hope this post helps you.....Enjoy!


No comments:

Post a Comment