I have a gridview, some of column is hidden but when i export to excel, in excel it still show the hidden column. May i know how to do it??
below is my sample script in App_Code:
public class GridViewExportUtil
{
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}
}
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
else if (current.Visible == false)
{
control.Controls.Remove(current);
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
Maybe you can do like this
GridView1.AllowSorting = false;
GridView1.Columns[2].Visible = false;
//export process
GridView1.Visible = true;
GridView1.AllowSorting = true;
oned_gk
Maybe you can do like this
GridView1.AllowSorting = false;
GridView1.Columns[2].Visible = false;
//export process
GridView1.Visible = true;
GridView1.AllowSorting = true;
Thank for your information. I have try to add in the below code, but the problem still same....any idea??
for (int i = gv.Columns.Count - 1; i >= 0; i--)
{
if (gv.Columns[i].Visible == false)
{
gv.HeaderRow.Cells[i].Visible = false;
gv.FooterRow.Cells[i].Visible = false;
}
gv.Columns[i].Visible = false;
}
Hi lansishao,
I have checked your code and to me, the method PrepareControlForExport(Control control) needs some refinement, actually when u replace different controls like link-buttons, dropdowns etc with LiteralControl controls, there you should check the visibility
of the actual control, if actual control is visible then you should replace it with Literal otherwise no need to add it. Hopefully you got my point,
else if (current is CheckBox && current.Visible)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
// Controls has been replace with Literal and Literal is alwasy Visible :)
else if (current.Visible == false)
{
control.Controls.Remove(current);
}
Thanks
Thank for your information. But actually i no so understand what you mean. Can you provide me some sample of code? thank you.
here it is,code id Bold and and under-lined
else if (current is CheckBox && current.Visible) // visibility check
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
// Controls has been replace with Literal and Literal is alwasy Visible :)
else if (current.Visible == false)
{
control.Controls.Remove(current);
}
Thank you...but after i try ur method add on the code, after export the data to excel, those hidden column still show....

can you share the ASPX page code, where you are hiding the grid columns, like grid item template and specially the hidden column template.
thanks
this is sample as below:
EnableModelValidation="True" CellPadding="4" ForeColor="#333333" GridLines="Vertical"
style="font-family: 'Century Gothic'; font-size: small" Width="98%" ShowFooter="True"
OnPreRender = "gv_PreRender" OnRowDataBound = "gv_RowDataBound">
ItemStyle-HorizontalAlign="Right"/>
ItemStyle-HorizontalAlign="Right"/>
change your hidden item template as follow,
ItemStyle-HorizontalAlign="Right"/>
Visible = "false" runat="server" Text='<%#Eval("Jan") %>'>
ItemStyle-HorizontalAlign="Right"/>
Visible = "false" runat="server" Text='<%#Eval("Feb") %>'>
thanks for you...i have set the label to visible and the problem solved..
because it have many page...may i know any best way to amend the code what i post before?
it is good to know that your problem solved, please mark the thread as answered if it helps you 
Secondly, in your code you are checking at control Level that if it is visible or not, your code line
if(control.visible==false)
so you have to apply the visible attribute to every control in every grid in every page, other wise chagne the code to export data, that change of code, may introduce other problems :)
ars88
if using
'<%#Eval("FieldNameInDataBase")%>'
if link button using