Export Gridview Record to Excel in C#

Coding:

=> Demo.aspx Source code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="architecture.Demo" %>  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.   
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7.   
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.         <div> <br /> <br />  
  11.             <asp:GridView ID="gvlist" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333">  
  12.                 <Columns>  
  13.                     <asp:BoundField DataField="ID" HeaderText="Id" HeaderStyle-Width="200px" />  
  14.                     <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="200px" />  
  15.                     <asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="200px" /> </Columns>  
  16.                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  17.                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> </asp:GridView> <br />  
  18.             <asp:Button ID="btnExport" runat="server" OnClick="Button1_Click" Text="Export To Excel" /> </div> <br /> </br>  
  19.     </form>  
  20. </body>  
  21.   
  22. </html>  
=>Demo.aspx.cs code behind file
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. using System.Collections;  
  12. namespace architecture  
  13. {  
  14.     public partial class Demo: System.Web.UI.Page   
  15.     {  
  16.         SqlConnection con;  
  17.         DataTable dt;  
  18.         //Connection String from Web.config file  
  19.         public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  20.         protected void Page_Load(object sender, EventArgs e) {  
  21.                 //lblTime.Text = DateTime.Today.DayOfWeek.ToString();  
  22.                 if (!IsPostBack) {  
  23.                     ShowData();  
  24.                 }  
  25.             }  
  26.             //method for Displaying Data in Gridview  
  27.         protected void ShowData()   
  28.         {  
  29.             SqlConnection con = new SqlConnection(constr);  
  30.             string str = "Select * from TbDemo; select * from TbDemo";  
  31.             con.Open();  
  32.             SqlCommand cmd = new SqlCommand(str, con);  
  33.             DataTable dt = new DataTable();  
  34.             SqlDataReader reader = cmd.ExecuteReader();  
  35.             dt.Load(reader);  
  36.             DataView dv = dt.DefaultView;  
  37.             gvlist.DataSource = dv;  
  38.             gvlist.DataBind();  
  39.             int i = 0;  
  40.             con.Close();  
  41.         }  
  42.         protected void Button1_Click(object sender, EventArgs e)  
  43.         {  
  44.             Response.ClearContent();  
  45.             Response.AddHeader("content-disposition""attachment;filename=Users.xls");  
  46.             Response.ContentType = "applicatio/excel";  
  47.             StringWriter sw = new StringWriter();;  
  48.             HtmlTextWriter htm = new HtmlTextWriter(sw);  
  49.             gvlist.RenderControl(htm);  
  50.             Response.Write(sw.ToString());  
  51.             Response.End();  
  52.         }  
  53.         public override void VerifyRenderingInServerForm(Control control) {}  
  54.     }  
  55. }  
Ebook Download
View all
Learn
View all