Export GridView data to txt file in Asp.net using C#

How to Export GridView to txt file using C# :

In this Post we learn how to export data from Grid View to Text file (.txt format) in ASP.Net using C#. When we are developing asp.net web site basically commercial web site then we needs to print the list of records which are show in grid view. As we know that we easily bind the asp.net grid view control for database for display the data in web page. So if we want to print the Grid view data in to text file. Then we can use this type of code.
Here we make a gridview in asp.net web page and then export data in a .text file.

Create a Gridview and Export data:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="ProductId" HeaderText="Product Id" ItemStyle-Width="100">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="ProductName" HeaderText="Product Name" ItemStyle-Width="150">
                    <ItemStyle Width="150px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Width="150">
                    <ItemStyle Width="150px"></ItemStyle>
                </asp:BoundField>
            </Columns>
            <HeaderStyle BackColor="Blue" ForeColor="#990000" BorderColor="#0000CC"
                BorderStyle="Groove" />
        </asp:GridView>

Make a DataTable and bind Grid by C# code:

DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ProductId"), new DataColumn("ProductName"), new DataColumn("Price") });
            dt.Rows.Add(1, "Lux", "10Rs");
            dt.Rows.Add(2, "Mobile", "10000Rs");
            dt.Rows.Add(3, "Sugar", "50/kg");
            dt.Rows.Add(4, "Biskit", "20Rs");
            GridView1.DataSource = dt;
            GridView1.DataBind();

C# Function of Exporting Grid Data:

ExportTextFile(object sender, EventArgs e)
{   
    string txt = string.Empty;

    foreach (TableCell cell in GridView1.HeaderRow.Cells)
    {       
        txt += cell.Text + "\t\t";
    }
   
    txt += "\r\n";

    foreach (GridViewRow row in GridView1.Rows)
    {
        //Making the space beween cells.
        foreach (TableCell cell in row.Cells)
        {          
            txt += cell.Text + "\t\t";
        }
       
        txt += "\r\n";
    }
   
    Response.Clear();
    Response.Buffer = true;
    //here you can give the name of file.
    Response.AddHeader("content-disposition", "attachment;filename=ProductList.txt");
    Response.Charset = "";
    Response.ContentType = "application/text";
    Response.Output.Write(txt);
    Response.Flush();
    Response.End();
}

Export Grid View to txt file using C# :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Exportdataintextfile.aspx.cs" Inherits=" Exportdataintextfile " %>

<!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 runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        table
        {
            border: 1px solid #ccc;
        }
        table th
        {
            background-color: #F7F7F7;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border-color: #ccc;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="ProductId" HeaderText="Product Id" ItemStyle-Width="100">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="ProductName" HeaderText="Product Name" ItemStyle-Width="150">
                    <ItemStyle Width="150px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Width="150">
                    <ItemStyle Width="150px"></ItemStyle>
                </asp:BoundField>
            </Columns>
            <HeaderStyle BackColor="Blue" ForeColor="#990000" BorderColor="#0000CC"
                BorderStyle="Groove" />
        </asp:GridView>
        <br />
        <asp:Button Text="Export in Text File" OnClick="ExportTextFile"
            runat="server" />
    </div>
    </form>
</body>
</html>


C# Code for Export Gridview:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Exportdataintextfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ProductId"), new DataColumn("ProductName"), new DataColumn("Price") });
            dt.Rows.Add(1, "Lux", "10Rs");
            dt.Rows.Add(2, "Mobile", "10000Rs");
            dt.Rows.Add(3, "Sugar", "50/kg");
            dt.Rows.Add(4, "Biskit", "20Rs");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

protected void ExportTextFile(object sender, EventArgs e)
{   
    string txt = string.Empty;

    foreach (TableCell cell in GridView1.HeaderRow.Cells)
    {       
        txt += cell.Text + "\t\t";
    }
   
    txt += "\r\n";

    foreach (GridViewRow row in GridView1.Rows)
    {
        //Making the space beween cells.
        foreach (TableCell cell in row.Cells)
        {          
            txt += cell.Text + "\t\t";
        }
       
        txt += "\r\n";
    }
   
    Response.Clear();
    Response.Buffer = true;
    //here you can give the name of file.
    Response.AddHeader("content-disposition", "attachment;filename=ProductList.txt");
    Response.Charset = "";
    Response.ContentType = "application/text";
    Response.Output.Write(txt);
    Response.Flush();
    Response.End();
}
}


Export GridView data to txt file in Asp.net using C#

Asp.net related Post :


Comments

Popular posts from this blog