In this article I will explain how to export data (records) from GridView to Text (Notepad) file in ASP.Net using C# and VB.Net.
A loop will be executed over the GridView rows to generate a concatenated string which finally is exported to a Text file which can be viewed in Notepad.
 
HTML Markup
The HTML markup consists of a GridView and a Button.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" ItemStyle-Width="100" />
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export" OnClick="ExportTextFile" runat="server" />
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the GridView
The GridView is populated using some dummy records using DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
Export GridView data to text file in ASP.Net using C# and VB.Net
 
 
Exporting GridView data to text file in ASP.Net using C# and VB.Net
When the Export button is clicked, the following event handler is executed.
Then a loop is executed over the Columns and Rows of the GridView and a concatenated string of records is generated which is separated by TAB character. You can also use any other separator character as per your choice.
Finally concatenated string is written to the Response which initiates the File download.
C#
protected void ExportTextFile(object sender, EventArgs e)
{
 
    //Build the Text file data.
    string txt = string.Empty;
 
    foreach (TableCell cell in GridView1.HeaderRow.Cells)
    {
        //Add the Header row for Text file.
        txt += cell.Text + "\t\t";
    }
 
    //Add new line.
    txt += "\r\n";
 
    foreach (GridViewRow row in GridView1.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            //Add the Data rows.
            txt += cell.Text + "\t\t";
        }
 
        //Add new line.
        txt += "\r\n";
    }
 
    //Download the Text file.
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.txt");
    Response.Charset = "";
    Response.ContentType = "application/text";
    Response.Output.Write(txt);
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub ExportTextFile(sender As Object, e As EventArgs)
 
    'Build the Text file data.
    Dim txt As String = String.Empty
 
    For Each cell As TableCell In GridView1.HeaderRow.Cells
        'Add the Header row for Text file.
        txt += cell.Text + vbTab & vbTab
    Next
 
    'Add new line.
    txt += vbCr & vbLf
 
    For Each row As GridViewRow In GridView1.Rows
        For Each cell As TableCell In row.Cells
            'Add the Data rows.
            txt += cell.Text + vbTab & vbTab
        Next
 
        'Add new line.
        txt += vbCr & vbLf
    Next
 
    'Download the Text file.
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.txt")
    Response.Charset = ""
    Response.ContentType = "application/text"
    Response.Output.Write(txt)
    Response.Flush()
    Response.End()
End Sub
 
The downloaded Text file containing the SQL Server data is shown below.
Export GridView data to text file in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads