Export Gridview in Word,Excel and Pdf
Export Gridview in Word,Excel and Pdf
We mainly need to export data, when need to maintain its reports.So we can export data in any form like(Word,Excel and Pdf).Its very Easy to export gridview content in any format like(Word,Excel and Pdf).
For this You need to do some simple task like..
firstly you need to create gridview on .aspx page and populate the gridview with your data.
Here i am using two control of .net Grdiview for populating data and Linkbutton for Download.
For working on this you have to be download ItextSharp.dll and then deploy it in ur project BIn folder.
.apsx
<asp:GridView ID="gr1" runat="server" AutoGenerateColumns="false"
onrowdatabound="gr1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="eid">
<ItemTemplate>
<asp:Label ID="empid" runat="server" Text='<%#Eval("EmployeeId") %>'>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="empid" runat="server" Text='<%#Eval("EmpFName") %>'>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export GridView as (Word/Excel/Pdf)</asp:LinkButton>
.Cs
For GridView Data....
Public Void Bindgr()
{
string qu = "select * from Profile_Detail WHERE (EmpFName <> '')";
SqlDataAdapter ad = new SqlDataAdapter(qu, con);
DataTable dt = new DataTable();
ad.Fill(dt);
gr1.DataSource = dt;
gr1.DataBind();
}
For Export Data Gridview as Word..
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition",
"attachment;filename=Information.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();
gr1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gr1);
frm.RenderControl(htmlWrite);
//GridView1.RenderControl(htw);
Response.Write(stringWrite.ToString());
Response.End();
}
For Export Data Gridview as Excel...
protected void LinkButton1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Information.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();
gr1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gr1);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
For Export Data Gridview as Pdf...
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Clear();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gr1.RenderControl(htw);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment; filename=Information.pdf");
Response.Write(sw.ToString());
Response.End();
}
Export GridView to PDF document in C#
ReplyDelete