Export GridView Data To Text File

The following is my DataTable in design mode from which I will show data in a GridView.
 
 sql table
 
The following is the script of my table:
  1. CREATE TABLE [dbo].[Employee](  
  2.     [Emp_ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Name] [varchar](50) NULL,  
  4.     [Designation] [varchar](50) NULL,  
  5.     [City] [varchar](50) NULL,  
  6.     [State] [varchar](50) NULL,  
  7.     [Country] [varchar](50) NULL,  
  8.  CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [Emp_ID] ASC  
  11. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13.   
  14. GO   
The following is the data in my table:
 
table data 
 
The following is my aspx code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>EXPORT Grid View To a Text File in ASP.NET C#</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <table style="border: solid 15px blue; width: 100%; vertical-align: central;">  
  12.             <tr>  
  13.                 <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;  
  14.                     font-size: 20pt; color: orangered;">  
  15.                     EXPORT Grid View To a Text File in ASP.NET C#  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td style="text-align: left; padding-left: 50px; border: solid 1px red;">  
  20.                     <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="90%"  
  21.                         BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"  
  22.                         CellPadding="4" GridLines="Both">  
  23.                         <Columns>  
  24.                             <asp:BoundField DataField="Name" HeaderText="Employee Name" />  
  25.                             <asp:BoundField DataField="Designation" HeaderText="Designation" />  
  26.                             <asp:BoundField DataField="City" HeaderText="City" />  
  27.                             <asp:BoundField DataField="State" HeaderText="State" />  
  28.                             <asp:BoundField DataField="Country" HeaderText="Country" />  
  29.                         </Columns>  
  30.                         <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
  31.                         <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
  32.                         <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />  
  33.                         <RowStyle BackColor="White" ForeColor="#003399" />  
  34.                         <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
  35.                     </asp:GridView>  
  36.                 </td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td align="right">  
  40.                     <asp:Button ID="btnExport" Text="Export Grid View" OnClick="btnExport_Click" runat="server" />  
  41.                 </td>  
  42.             </tr>  
  43.         </table>  
  44.     </div>  
  45.     </form>  
  46. </body>  
  47. </html>  
The following is the aspx.cs code:
  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.Data.SqlClient;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     SqlDataAdapter da;  
  13.     DataSet ds = new DataSet();  
  14.     DataTable dt = new DataTable();  
  15.   
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.         if (!Page.IsPostBack)  
  19.             this.BindGrid();  
  20.     }  
  21.   
  22.     private void BindGrid()  
  23.     {  
  24.         SqlConnection con = new SqlConnection();  
  25.         ds = new DataSet();  
  26.         con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";  
  27.         SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);  
  28.   
  29.         da = new SqlDataAdapter(cmd);  
  30.         da.Fill(ds);  
  31.         con.Open();  
  32.         cmd.ExecuteNonQuery();  
  33.         con.Close();  
  34.   
  35.         if (ds.Tables[0].Rows.Count > 0)  
  36.         {  
  37.             GridViewEmployee.DataSource = ds.Tables[0];  
  38.             GridViewEmployee.DataBind();  
  39.         }  
  40.     }  
  41.   
  42.     protected void btnExport_Click(object sender, EventArgs e)  
  43.     {  
  44.         string txtFile = string.Empty;  
  45.   
  46.         //Adding Column Name In Text File.  
  47.         foreach (TableCell cell in GridViewEmployee.HeaderRow.Cells)  
  48.         {              
  49.             txtFile += cell.Text + "\t\t";  
  50.         }  
  51.           
  52.         txtFile += "\r\n";  
  53.   
  54.         //Adding Data Column Values in Text File  
  55.         foreach (GridViewRow row in GridViewEmployee.Rows)  
  56.         {  
  57.             foreach (TableCell cell in row.Cells)  
  58.             {                  
  59.                 txtFile += cell.Text + "\t\t";  
  60.             }               
  61.             txtFile += "\r\n";  
  62.         }  
  63.            
  64.         Response.Clear();  
  65.         Response.Buffer = true;  
  66.         Response.AddHeader("content-disposition""attachment;filename=EmployeeData.txtFile");  
  67.         Response.Charset = "";  
  68.         Response.ContentType = "application/text";  
  69.         Response.Output.Write(txtFile);  
  70.         Response.Flush();  
  71.         Response.End();  
  72.     }  
  73. }  
Now run the application. 
 
export grid 
 
Employee data txt
 
exported txt file 


Similar Articles