Click here to Skip to main content
Click here to Skip to main content

All in One Export Data in ASP.NET

, 22 May 2011 CPOL
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 . .
Follow on   Twitter

Comments and Discussions

 
Questionwhat is gv in the code ??<pre lang="c++"></pre> PinmemberMember 105441282-May-14 12:30 
GeneralGreat article PinmemberFlorentinB11-Feb-14 23:48 
Question[My vote of 1] it is showing html tags in word file. Pinprofessionalabdussalam14328-Nov-13 2:40 
GeneralMy vote of 5 Pinmemberrmksiva18-Apr-13 4:41 
GeneralExporting GridView data to PDF with sugession Pinmembervaraprasadreddy.bh30-Aug-12 6:13 
QuestionBetter HTML to PDF export PinmemberMember 902839525-May-12 3:30 
QuestionExport with formatting to PDF PinmemberVrushali Sawant23-Apr-12 2:33 
QuestionExcel export in ASP.NET PinmemberCikaPero19-Jul-11 23:25 
GeneralError in line frm.RenderControl(htw); Pinmembermarianomolina1514-Jun-11 12:29 
RantMy, but aren't we supportive. PinmemberBrian Stevens24-May-11 18:55 
GeneralMy vote of 5 PinmemberBrian Stevens24-May-11 18:49 
Generalwarnings etc Pinmemberdave.dolan23-May-11 5:47 
GeneralNot an article Pinmemberdigital man23-May-11 1: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.1411023.1 | Last Updated 22 May 2011
Article Copyright 2011 by prabhakarparihar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid