Вы находитесь на странице: 1из 3

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.

cs"
Inherits="ExportGridToExcel._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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<div>
<asp:GridView ID="GridView" runat="server" CellPadding="6" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
/>
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City"
/>
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="Designation" HeaderText="Designation"
SortExpression="Designation" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

</asp:GridView>
<br />
&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp<asp:Button ID="Button1"
runat="server" Text="Export To Excel" onclick="Button1_Click" />

</div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"


ConnectionString="<%$ ConnectionStrings:getconn %>"
SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>

</form>
</body>
</html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Web;

namespace ExportGridToExcel
{
public partial class _Default : System.Web.UI.Page
{
private SqlConnection con;
private SqlCommand com;
private string constr, query;
private void connection()
{
constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
con = new SqlConnection(constr);
con.Open();

}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bindgrid();

}
}

public override void VerifyRenderingInServerForm(Control control)


{

private void Bindgrid()


{
connection();
query = "select *from Employee";
com = new SqlCommand(query, con);
SqlDataReader dr = com.ExecuteReader();

con.Close();

}
protected void Button1_Click(object sender, EventArgs e)
{
ExportGridToExcel();
}
private void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "FILE" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
GridView.GridLines = GridLines.Both;
GridView.HeaderStyle.Font.Bold = true;
GridView.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();

}
}

Вам также может понравиться