How to export Grid View Data to PDF or Excel?


In this article I have explained about how to export Grid view data into Excel and PDF format. We can easily export grid view data to excel format without use any reference but we need some reference for export as PDF. In this example I used itextsharp.dll reference for export as PDF

Description:
In ASP.Net application we show database records in the Grid View some time clients need to save data as PDF or Excel format. So here I provide option to save grid view data into a PDF or Excel format.

How to Export data as MS Excel format?
Client side placed one grid view and two buttons.
Below code is for export grid view data to MS Excel


protected void Button1_Click(object sender, EventArgs e)
{
//here session value is stored before bind data on gridview
DataTable dt = Session["source_table"] as DataTable;
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyy") + ".xls");
Response.ContentType = "application/ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}


How to Export data as PDF format?
Below code is for export grid view data to PDF format

public void ExportToPdf(DataTable ExDataTable)
{
//Here set page size as A4

Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();

//Set Font Properties for PDF File
Font fnt = FontFactory.GetFont("Times New Roman", 12);
DataTable dt = ExDataTable;

if (dt != null)
{

PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;

//Here we create PDF file tables

for (int rows = 0; rows < dt.Rows.Count; rows++)
{
if (rows == 0)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].ColumnName.ToString(), fnt)));
PdfTable.AddCell(PdfPCell);
}
}
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt)));
PdfTable.AddCell(PdfPCell);
}
}

// Finally Add pdf table to the document
pdfDoc.Add(PdfTable);
}

pdfDoc.Close();

Response.ContentType = "application/pdf";

//Set default file Name as current datetime
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");

System.Web.HttpContext.Current.Response.Write(pdfDoc);

Response.Flush();
Response.End();

}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

Source Code Detail:
Here with I have attached entire source code. Download it and try to export grid view data as PDF format or Excel format.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this Article is help to you for Export Grid view data to PDF/Excel.


Attachments

  • Grid_View_Export_PDF_or_Excel_format (42818-15117-Grid_viewExport.rar)
  • Comments

    Guest Author: vara prasad reddy30 Aug 2012

    Hi Hear is the solution with details and solution for the problems when exporting

    http://www.reddyinfosoft.blogspot.in/2012/08/export-gridview-data-to-pdf-using.html#more



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: