Introduction
In this article i will explain you how to export (Download) all Grid data in PDF(PORtable Document Format) file.
Description
Here i repeat the same process as i did my previous update to export (Download) all Grid data in CSV(Comma Separated Values) file.
Database Design
Create Database EmployeeDb
Use EmployeeDb
Create table Employee( Id int Identity(1,1), Name varchar(50),EmailId nvarchar(250), Address varchar(max) , MobileNo int )
Aspx page View
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <br /> <br />
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="EmailID" HeaderText="Email Id" ItemStyle-Width="150px" />
<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="140px" />
<asp:BoundField DataField="MobileNo" HeaderText="Mobile No" ItemStyle-Width="140px" />
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export To PDF" /> </div> <br /> <br/>
</form>
</body>
</html>
After completing the design phase go to view code window and write the code given below
For exporting grid data in PDF we have to use iText library. According to Itext description iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documnets in the PRtable Document Format(PDF).
You don't have itext library on your project, to get iText library on your project use microsoft NuGet package Manager.
Goto Tools => NuGet package Manager.=> Manage NuGet package for solution ==> on serach tab type iText and hit search and installed the library for your project.
Namespaces
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
CS page View
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered*/
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
//method for Displaying Data in Gridview
protected void BindGrid()
{
SqlConnection con = new SqlConnection("Data Source=JP-PC\\JP;Initial Catalog=EmployeeDb;Integrated Security=true;");
SqlCommand cmd = new SqlCommand("SELECT * FROM Employee", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
con.Close();
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType
= "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvUserInfo.AllowPaging = false;
gvUserInfo.DataBind();
gvUserInfo.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htw = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htw.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
In above code you can see i used a VerifyRenderingInServerForm function. The purpose of this function is avoid the error like "control must be placed in inside of form tag". If we set VerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly.
Create Database EmployeeDb
Use EmployeeDb
Create table Employee( Id int Identity(1,1), Name varchar(50),EmailId nvarchar(250), Address varchar(max) , MobileNo int )
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <br /> <br />
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="EmailID" HeaderText="Email Id" ItemStyle-Width="150px" />
<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="140px" />
<asp:BoundField DataField="MobileNo" HeaderText="Mobile No" ItemStyle-Width="140px" />
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export To PDF" /> </div> <br /> <br/>
</form>
</body>
</html>
After completing the design phase go to view code window and write the code given below
For exporting grid data in PDF we have to use iText library. According to Itext description iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documnets in the PRtable Document Format(PDF).
You don't have itext library on your project, to get iText library on your project use microsoft NuGet package Manager.
Goto Tools => NuGet package Manager.=> Manage NuGet package for solution ==> on serach tab type iText and hit search and installed the library for your project.
Namespaces
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
CS page View
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered*/
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
//method for Displaying Data in Gridview
protected void BindGrid()
{
SqlConnection con = new SqlConnection("Data Source=JP-PC\\JP;Initial Catalog=EmployeeDb;Integrated Security=true;");
SqlCommand cmd = new SqlCommand("SELECT * FROM Employee", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
con.Close();
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvUserInfo.AllowPaging = false;
gvUserInfo.DataBind();
gvUserInfo.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htw = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htw.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
In above code you can see i used a VerifyRenderingInServerForm function. The purpose of this function is avoid the error like "control must be placed in inside of form tag". If we set VerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly.
The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Projects for CSE.
ReplyDeleteSoftware management seeks for decision support to identify technologies like JavaScript that meet best the goals and characteristics of a software project or improvement programme. JavaScript Training in Chennai Accessible experiences and repositories that effectively guide that technology selection are still lacking.
Aim of technology domain analysis is to describe the class of context situations (e.g., kinds of JavaScript software projects) in which a software engineering technology JavaScript Training in Chennai can be applied successfully
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training