Preethi Ganesh

Preethi Ganesh

  • NA
  • 11
  • 8.8k

GridView with Filter Export to Excel

Jun 29 2016 1:15 AM
Hi!
 
I have a asp.net gridview with filter on each of the column the Header. If I export it to excel, the cells with the filter(like text box, dropdownlist) also appears in Excel. How can I seperate the filter from the header while exporting to excel?
 
 My Code below: 
 
protected void ExportCSV(object sender, EventArgs e)
{
 
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
gvRecipeSetup.AllowPaging = false;
this.FilterWithPaging();
gvRecipeSetup.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in gvRecipeSetup.HeaderRow.Cells)
{
cell.BackColor = gvRecipeSetup.HeaderStyle.BackColor;
}
foreach (GridViewRow row in gvRecipeSetup.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = gvRecipeSetup.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = gvRecipeSetup.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
gvRecipeSetup.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
 

Answers (1)