HTML
<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>
CODE BEHIND FILE
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","red");
}
}
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;
gvClientList.DataBind();
}
protected void btnExportoWordFormat_Click(object sender, EventArgs e)
{
Response.ClearContent();
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);
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; }