Gridview to Excel File (Download)
Gridview to Excel Downlaod
ASPX.CS
Library files
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.Data.SqlClient;
using System.Configuration;
using System.IO;
-----------
CS code
-----------
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment; filename=Filename.xls");
Response.ContentType = "application/excel";
StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlTextwriter = new HtmlTextWriter(stringwriter);
// Set white color as the background color for gridview header row
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
// Set background color of each cell of GridView1 header row
foreach (TableCell tableCell in GridView1.HeaderRow.Cells)
{
tableCell.Style["background-color"] = "#A55129";
}
// Set background color of each cell of each data row of GridView1
foreach (GridViewRow gridViewRow in GridView1.Rows)
{
gridViewRow.BackColor = System.Drawing.Color.White;
foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)
{
gridViewRowTableCell.Style["background-color"] = "#FFF7E7";
}
}
GridView1.RenderControl(htmlTextwriter);
Response.Write(stringwriter.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Comments
Post a Comment