Click here to Skip to main content
11,640,125 members (67,879 online)
Click here to Skip to main content

All in One Export Data in ASP.NET

, 22 May 2011 CPOL 75.1K 50
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

Introduction

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.Cache.SetCacheability(HttpCacheability.NoCache);
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();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htmlWrite);
//GridView1.RenderControl(htw);
Response.Write(stringWrite.ToString());
Response.End();

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.ClearContent();
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();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

After Click, one Save As Dialog Box opens.

Export to PDF

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

Link: http://sourceforge.net/projects/itextsharp/

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");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(hw);
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);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

After Click, one Save As Dialog Box opens:

License

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

Share

About the Author

prabhakarparihar
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
Member 1123902028-Nov-14 18:15
memberMember 1123902028-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
memberrmksiva18-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
digital man23-May-11 0:04
memberdigital man23-May-11 0:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 22 May 2011
Article Copyright 2011 by prabhakarparihar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid