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

how to export gridview data to word in using c#

Dear Viewers sometimes we need to export our grid view data to word format to view as a report or to transfer it easily such situation can be solved easily in 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 case some extra namespaces needs to be imported such as System. Data, System.IO, System.Web.UI, System.Data

Export gridview data into ms-word format


<form id="form1" runat="server">
  <asp:GridView ID="gvClientList" AutoGenerateColumns="false" CellPadding="5" runat="server">
  <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" />
  <HeaderStyle BackColor="#6699FF" Font-Bold="true" ForeColor="White" />
  <FooterStyle BackColor="#3366CC" />
  <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 />


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;


protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
  /* 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++)
  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 Mozumder", "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("Sumaya Akter", "01916751804", "Sirajgonj", 50000));
  ClientList.Add(new Client("Siful Islam", "01916751808", "Dhaka", 1000000));
  ClientList.Add(new Client("Mizanur Rahman", "01916751807", "Dhaka", 20000));
  gvClientList.DataSource = ClientList;
  protected void btnExportoWordFormat_Click(object sender, EventArgs e)
  Response.Buffer = true;
  Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ClientList.doc"));
  Response.ContentType = "application/ms-word";
  StringWriter sw = new StringWriter();
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  //Grid View Header row backcolor styling function
  //Grid View Cell styling function
  //Grid View Column styling function
  //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


Output ms-word format


Realted Article Headline

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

Article Category

How to create control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in 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# c # basic tutorial
How to use ajax toolkit in C#
How to use different types of validation control using c#
How to use grid view in c#
Protected by Copyscape Online Plagiarism Detection