Wednesday 17 September 2014

How to Export GridView Data to Pdf in Asp.Net


How to Export GridView Data to Pdf in Asp.Net……………………….

Note :In this topic we will discuss how to export GridView Data to Pdf in Asp.Net

Step 1: First  we will go to  this link   http://sourceforge.net/projects/itextsharp/ and  Download here itextsharp.dll and follow this step …………………………….

Right Click on WebSte>Add References ->



After that we can see this itextsharp.dll in Bin Folder

--sql query………………………………………….

create database GridConcept
use GridConcept
create table Employee
(
SrNo int identity(1,1) primary key,
Name varchar(50),
Gender varchar(50),
Address varchar(50),
MobileNo varchar(50)
)
Default.aspx ………………………………………

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<font color="blue"><b><center>How to Export GridView Data to Pdf in Asp.Net</center></b></font> </td>
</tr>
<tr>
<td >
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnexportpdf" runat="server" onclick="btnexportpdf_Click"
Text="Export with Pdf without Header" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnwithHeader" runat="server" Text="with Header without Color"
onclick="btnwithHeader_Click" />
&nbsp;&nbsp;
<asp:Button ID="btnwithHeaderwithColor" runat="server"
Text="with Header with Color" onclick="btnwithHeaderwithColor_Click" />
</td></tr>
</table>
</div>
</form>
</body>
</html>

Code with image ………………………………………


Default.aspx.cs ………………………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
// using this namespace Export GridView to Pdf
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// CODE FOR BIND GRIDVIEW ON PAGELOAD ................
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=KUSHTIWARI-PC;Initial Catalog=GridConcept;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Employee", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}

// Export GridView to Pdf without Header ................

protected void btnexportpdf_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (GridViewRow grv  in GridView1.Rows)
{
foreach(TableCell tc in grv.Cells )
{
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pt.AddCell(pc);
}
}
Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();
}

// Export GridView to pdf with Header but without GridView color................

protected void btnwithHeader_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headercell in GridView1.HeaderRow.Cells)
{
PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text));
pt.AddCell(pdfcell);
}
foreach (GridViewRow grv in GridView1.Rows)
{
foreach (TableCell tc in grv.Cells)
{
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pt.AddCell(pc);
}
}

Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();

}

// Export GridView to pdf with Header but with Color format................

protected void btnwithHeaderwithColor_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headercell in GridView1.HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);
PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text,font));
pdfcell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor);
pt.AddCell(pdfcell);
}

foreach (GridViewRow grv in GridView1.Rows)
{
foreach (TableCell tc in grv.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pc.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor);
pt.AddCell(pc);
}
}

Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();
}

// apply style sheet on rowdatabound command................
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String mno = DataBinder.Eval(e.Row.DataItem, "MobileNo").ToString();
if (mno.Length < 10)
{
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].Font.Bold = true;
e.Row.Cells[4].Font.Size = 15;
}
}
}
}

Result

0 comments:

Post a Comment