Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<br />
<center> <asp:Button ID="btn1" runat="server" Text="Export to Excel" OnClick="btn1_Click" />
<asp:Button ID="Button1" runat="server" Text="Export to Word"
onclick="Button1_Click"/>
<asp:Button ID="Button2" runat="server" Text="Export to PDF"
onclick="Button2_Click"/>
<input id="Submit1" onclick="window.print()" type="submit" value="Print" /><br />
<br />
<asp:GridView ID="gv1" runat="server">
</asp:GridView>
</center>
</asp:Content>
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;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
string sql = "select * from Employee_Details";
SqlCommand cmd = new SqlCommand(sql, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
gv1.DataSource = ds;
gv1.DataBind();
}
protected void btn1_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment; filename=Expport.xls");
Response.ContentType = "application/ms-excel";
StringWriter sWriter = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
HtmlForm frm = new HtmlForm();
gv1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv1);
frm.RenderControl(hTextWriter);
// EmpReportGrid.RenderControl(hTextWriter);
Response.Write(sWriter.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment; filename=Expport.doc");
Response.ContentType = "application/ms-word";
StringWriter sWriter = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
HtmlForm frm = new HtmlForm();
gv1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv1);
frm.RenderControl(hTextWriter);
// EmpReportGrid.RenderControl(hTextWriter);
Response.Write(sWriter.ToString());
Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment; filename=Expport.pdf");
Response.ContentType = "application/pdf";
StringWriter sWriter = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
HtmlForm frm = new HtmlForm();
gv1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv1);
frm.RenderControl(hTextWriter);
// EmpReportGrid.RenderControl(hTextWriter);
Response.Write(sWriter.ToString());
Response.End();
}
}
web.config
<connectionStrings>
<add name="con" connectionString="Data Source=.;Initial Catalog=exp;Integrated Security=true;"/>
</connectionStrings>
Output
No comments:
Post a Comment