Click here to Skip to main content
13,905,337 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


52 bookmarked
Posted 22 May 2011
Licenced CPOL

All in One Export Data in ASP.NET

, 22 May 2011
Rate this:
Please Sign up or sign in to vote.
All in One Export Data in ASP.NET, Gridview to DOC – Gridview to Excel – Gridview to PDF


In this post, I will show the power to export data to other document software. In other software, you have a lot of work that can be done in an easy manner. For sorting, searching and security purposes, you do not want to give data Excel & Doc format, so you use Export GridView to PDF.

In this post, you can read some good tricks for exporting data in documents & PDF files.

In .aspx Page

In the .aspx page, firstly add controls, TextBox and Button and Gridview. First, I write a query in text box, then click GO button, fill a Gridview. Then I use three buttons export to Word, Export to Excel, Export to PDF.

Export to Word

In an export to Word, write this code on btnexport towards the Click event.

Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";

After click, one Save As Dialog Box opens:

Export to Excel

In an export to Excel, write this code on btnexporttoExcel Click event.

string attachment = "attachment; filename=Export.xls";
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";


After Click, one Save As Dialog Box opens.

Export to PDF

You want to export Gridview to PDF, so first add the itextsharp.dll.


Then add some namespaces:

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

In an export to PDF, write this code on btnexporttoPdf Click event.

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

After Click, one Save As Dialog Box opens:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prabhakar Parihar
Software Developer Mencentric Infotech Pvt.Ltd.
India India
I am Software Developer . .

You may also be interested in...


Comments and Discussions

QuestionExport Panel Content to pdf not working Pin
Manish Goswami28-Nov-14 18:15
memberManish Goswami28-Nov-14 18:15 
Questionwhat is gv in the code ??<pre lang="c++"></pre> Pin
Member 105441282-May-14 11:30
memberMember 105441282-May-14 11:30 
Question[My vote of 1] it is showing html tags in word file. Pin
abdussalam14328-Nov-13 1:40
professionalabdussalam14328-Nov-13 1:40 
GeneralMy vote of 5 Pin
rmksiva18-Apr-13 3:41
professionalrmksiva18-Apr-13 3:41 
GeneralExporting GridView data to PDF with sugession Pin
varaprasadreddy.bh30-Aug-12 5:13
membervaraprasadreddy.bh30-Aug-12 5:13 
QuestionBetter HTML to PDF export Pin
Member 902839525-May-12 2:30
memberMember 902839525-May-12 2:30 
QuestionExport with formatting to PDF Pin
Vrushali Sawant23-Apr-12 1:33
memberVrushali Sawant23-Apr-12 1:33 
QuestionExcel export in ASP.NET Pin
CikaPero19-Jul-11 22:25
memberCikaPero19-Jul-11 22:25 
GeneralError in line frm.RenderControl(htw); Pin
marianomolina1514-Jun-11 11:29
membermarianomolina1514-Jun-11 11:29 
RantMy, but aren't we supportive. Pin
Brian Stevens24-May-11 17:55
memberBrian Stevens24-May-11 17:55 
GeneralMy vote of 5 Pin
Brian Stevens24-May-11 17:49
memberBrian Stevens24-May-11 17:49 
Generalwarnings etc Pin
dave.dolan23-May-11 4:47
memberdave.dolan23-May-11 4:47 
GeneralNot an article Pin
R. Giskard Reventlov23-May-11 0:04
professionalR. Giskard Reventlov23-May-11 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 22 May 2011
Article Copyright 2011 by Prabhakar Parihar
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid