Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

All in One Export Data in ASP.NET

0.00/5 (No votes)
22 May 2011 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here