Posted by : Anwar Hossain
Category : How to use grid view in asp.net c#

how to export gridview data to excel in c#

Dear Viewers sometimes we need to export our grid view data to excel format to view as a report or to transfer it easily such situation can be solved easily at asp.net using c# .Firstly I have created List type class to supply data to grid view then after binding data to grid view some related function also be created to implement the task. In this some extra namespace needs to be imported such as System. Data, System. IO, System. Web. UI, System. Data

Export gridview data into ms-excel format

HTML

<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvClientList" AutoGenerateColumns="false" CellPadding="5" runat="server">
  <Columns>
  <asp:BoundField HeaderText="Client Name" DataField="_ClientName" />
  <asp:BoundField HeaderText="Client Phone" DataField="_ClientPhone" />
  <asp:BoundField HeaderText="Adress " DataField="_ClientAddress" />
  <asp:BoundField HeaderText="Due Payment" DataField="_DuePayment" />
  </Columns>
  <HeaderStyle BackColor="#6699FF" Font-Bold="true" ForeColor="White" />
  <FooterStyle BackColor="#3366CC" />
  </asp:GridView>
  </div>
  <br />
  <asp:Button ID="btnExportotExcelformat" runat="server" Text="Export data to Excel"
  OnClick="btnExportotExcelformat_Click" BackColor="#99CC00"
  BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" ForeColor="#666633"
  Height="30px" />
  <br />
  </form>
</body>

NAMESPACE

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

CODE BEHIND

protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  AllClientList();
  }
  }
 
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
  server control at run time. */
  public override void VerifyRenderingInServerForm(Control control)
  {
 
  }
 
  private void ChangHeadrowBackColor()
  {
 
  gvClientList.HeaderRow.Style.Add("background-color", "#FFFFFF");
 
 
  }
  private void DesingGridViewCell()
  {
  //After counting the headerroewcell of the gridview design exported Headercell
  for (int i = 0; i < gvClientList.HeaderRow.Cells.Count; i++)
  {
  gvClientList.HeaderRow.Cells[i].Style.Add("background-color", "green");
 
 
  }
 
  }
 
  private void DesingGridViewColmun()
  {
 
  //After counting the cloums of the gridview design exported column
  for (int i = 0; i < gvClientList.Columns.Count; i++)
  {
  gvClientList.Columns[i].ItemStyle.BackColor = Color.AntiqueWhite;
  }
 
  }
 
  //Create temporarydata but real time data will be loaded form database
  private void AllClientList()
  {
 
  List<Client> ClientList = new List<Client>();
  ClientList.Add(new Client("Iqbal Hossain","01916751804","Noakhali",50000));
  ClientList.Add(new Client("Jabed Hossain", "01916751805", "Lakmipur", 70000));
  ClientList.Add(new Client("Fazlul Goni Roman", "01916751806", "Chandpur", 80000));
  ClientList.Add(new Client("Prince Hossain", "01916751807", "Rangpur", 90000));
  ClientList.Add(new Client("Mr Khorsed Alam", "01916751864", "Narayangonj", 50000));
  ClientList.Add(new Client("Sumay Akter", "01916751804", "Sirajgonj", 50000));
  ClientList.Add(new Client("Siful Islam", "01916751808", "Dhaka", 1000000));
  gvClientList.DataSource = ClientList;
  gvClientList.DataBind();
 
 
 
 
 
  }
 
  protected void btnExportotExcelformat_Click(object sender, EventArgs e)
  {
  Response.ClearContent();
  Response.Buffer = true;
  Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ClienentList.xls"));
  Response.ContentType = "application/ms-excel";
  StringWriter sw = new StringWriter();
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  AllClientList();
  //Grid View Header row backcolor styling function
  ChangHeadrowBackColor();
  //Grid View Cell styling function
  DesingGridViewCell();
  //Grid View Column styling function
  DesingGridViewColmun();
  gvClientList.RenderControl(htw);
  Response.Write(sw.ToString());
 
  Response.End();
  }
 
//Create class for temporarydata
public class Client
{
 
 
  public Client(string ClientName, string ClientPhone, string ClientAddress, decimal DuePayment)
  {
 
  _ClientName = ClientName;
  _ClientPhone = ClientPhone;
  _ClientAddress = ClientAddress;
  _DuePayment = DuePayment;
 
  }
 
  public string _ClientName { get; set; }
 
  public string _ClientPhone { get; set; }
 
  public string _ClientAddress { get; set; }
 
  public decimal _DuePayment { get; set; }
}
 

Out Put

how-to-export-gridview-data-to-excel-format-output

 

 

Output excel format

how-to-export-gridview-data-to-excel-format-output




Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using asp.net c#.
How to create grid view paging in asp.net using c#
How to bind Multi-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Two-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Single-Dimensional Arrays Data to gird view in asp.net using C#
how to export gridview data to pdf in asp.net c#
how to export gridview data to word in asp.net using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using asp.net c#
How to delete data from grid view using row deleting event in asp.net c#
How to bind data into gridview using code behind in asp.net c#
How to scroll gird view overflow data in asp.net c#
How to Bind data into grid view control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection