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