Advertisement
how to export data from gridview to pdf
in asp.net using c#
I will explain how to export data from
gridview to pdf in asp.net using c#.
I created a database with table name
EmployeeInfo as shown.
USE
[chinnu]
GO
/******
Object: Table [dbo].[EmployeeInfo] Script Date: 10/29/2013
00:13:02 ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
CREATE
TABLE
[dbo].[EmployeeInfo](
[EmpId]
[nvarchar](50)
NOT
NULL,
[EmpName]
[nvarchar](50)
NOT
NULL,
[EmpAddress]
[nvarchar](50)
NOT
NULL,
[EmpDesignation]
[nvarchar](50)
NOT
NULL
)
ON
[PRIMARY]
GO
In
Default.aspx I am adding GridView and ImageButton as Shown.
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="file_conversion._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>
</head>
<body>
<form
id="form1"
runat="server">
<asp:GridView
ID="gridview"
runat="server"
AutoGenerateColumns="False"
>
<Columns>
<asp:BoundField
DataField="EmpId"
HeaderText="EmployeeId"
InsertVisible="False"
/>
<asp:BoundField
DataField="EmpName"
HeaderText="EmployeeName"
InsertVisible="False"
/>
<asp:BoundField
DataField="EmpAddress"
HeaderText="EmployeeAddress"
/>
<asp:BoundField
DataField="EmpDesignation"
HeaderText="EmployeeDesignation"
/>
</Columns>
</asp:GridView>
<asp:ImageButton
ID="btnPDF"
runat="server"
ImageUrl="Scripts/download.jpg"
ToolTip
="Click
To Export PDF Format"
Width="32px"
Height="32px"
OnClick="btnPDF_Click"
/>
</form>
</body>
</html>
Now
I am writing the logic in Default.aspx.cs to export gridview data in
to pdf format as shown.
using
System;
using
System.Configuration;
using
System.Data;
using
System.IO;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
iTextSharp.text;
using
iTextSharp.text.pdf;
using
iTextSharp.text.html.simpleparser;
using
System.Data.SqlClient;
namespace
file_conversion
{
public
partial
class
_Default
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(!IsPostBack)
{
BindGridData();
}
}
private
void
BindGridData()
{
try
{
using
(SqlConnection
con = new
SqlConnection(ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString))
{
SqlCommand
command = new
SqlCommand("SELECT
* from EmployeeInfo",
con);
SqlDataAdapter
da = new
SqlDataAdapter(command);
DataTable
dt = new
DataTable();
da.Fill(dt);
gridview.DataSource
= dt;
gridview.DataBind();
}
}
catch
(Exception
ex)
{
}
}
protected
void
btnPDF_Click(object
sender, ImageClickEventArgs
e)
{
Response.ContentType
= "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=File.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter
sw = new
StringWriter();
HtmlTextWriter
htw = new
HtmlTextWriter(sw);
HtmlForm
hfrm = new
HtmlForm();
gridview.Parent.Controls.Add(hfrm);
hfrm.Attributes["runat"]
= "server";
hfrm.Controls.Add(gridview);
hfrm.RenderControl(htw);
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();
}
}
}
In
web.config I am adding the ConnectionString
<connectionStrings>
<add
name="Connstring"
connectionString="Data
Source=DotNetSharePoint;Database=Chinnu;User
Id=sa;Password=password123;"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
Now
Press F5.
OutPut:
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteDot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai
Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training