Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have a gridview with many columns, of which few are invisible.
I want to export only the visible coulmns of grid to excel .

May i know how cam i do this??

Thanks in advance
Posted

1 solution

I used this ExporToExcel utility.
and it also have a property ColumnToExclude by which you can exclude gridview columns that you do'nt want to export.
 
Share this answer
 
Comments
dhage.prashant01 18-Jun-13 5:37am    
I want to write my own code.
Any idea or any logic you can provide?
uspatel 18-Jun-13 5:49am    
Try
protected void Button1_Click(object sender, EventArgs e)
{
try
{

GridView1.AllowPaging = false;
GridView1.DataBind();

PrepareGridViewForExport(GridView1);

Context.Response.ClearContent();
Context.Response.ContentType = "application/ms-excel";
Context.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "enquiry_request_details"));
Context.Response.Charset = "";
System.IO.StringWriter stringwriter = new System.IO.StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
GridView1.RenderControl(htmlwriter);
Context.Response.Write(stringwriter.ToString());
Context.Response.End();

}
catch (Exception ex)
{

}
}
public override void VerifyRenderingInServerForm(Control control)
{

}
private void PrepareGridViewForExport(Control gridView)
{
for (int i = 0; i < gridView.Controls.Count; i++)
{
//Get the control
Control currentControl = gridView.Controls[i];
if (currentControl is LinkButton)
{
gridView.Controls.Remove(currentControl);
gridView.Controls.AddAt(i, new LiteralControl((currentControl as LinkButton).Text));
}
else if (currentControl is ImageButton)
{
gridView.Controls.Remove(currentControl);
gridView.Controls.AddAt(i, new LiteralControl((currentControl as ImageButton).AlternateText));
}
else if (currentControl is HyperLink)
{
gridView.Controls.Remove(currentControl);
gridView.Controls.AddAt(i, new LiteralControl((currentControl as HyperLink).Text));
}
else if (currentControl is DropDownList)
{
gridView.Controls.Remove(currentControl);
gridView.Controls.AddAt(i, new LiteralControl((currentControl as DropDownList).SelectedItem.Text));
}
else if (currentControl is CheckBox)
{
gridView.Controls.Remove(currentControl);
gridView.Controls.AddAt(i, new LiteralControl((currentControl as CheckBox).Checked ? "True" : "False"));
}
if (currentControl.HasControls())
{
// if there is any child controls, call this method to prepare for export
PrepareGridViewForExport(currentControl);
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900