Gridview export to excel without having control like linkbutton, or anybutton(to hide columns)
Frontend note:
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void ExporttoExcel()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExistingCarData" + System.DateTime.Now.ToShortDateString() + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
grvCarModel.AllowPaging = false;
GetCarsBind();
grvCarModel.Columns[1].Visible = false;
grvCarModel.Columns[7].Visible = false;
grvCarModel.Columns[8].Visible = false;
grvCarModel.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Records are saved!')", true);
}
Frontend note:
<%@ Page Title="" Language="C#"
MasterPageFile="~/admin/admin.master"
AutoEventWireup="true"
CodeFile="CarMake.aspx.cs" Inherits="admin_CarMake" EnableEventValidation="false" %>
CodeFile="CarMake.aspx.cs" Inherits="admin_CarMake" EnableEventValidation="false" %>
-----------------------------------------------------------------------------------------
Backend cs:-
protected void btnExport_Click(object sender, EventArgs
e)
{
ExporttoExcel();
}
{
ExporttoExcel();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void ExporttoExcel()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExistingCarData" + System.DateTime.Now.ToShortDateString() + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
grvCarModel.AllowPaging = false;
GetCarsBind();
grvCarModel.Columns[1].Visible = false;
grvCarModel.Columns[7].Visible = false;
grvCarModel.Columns[8].Visible = false;
grvCarModel.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Records are saved!')", true);
}
Comments
Post a Comment