For export to excel here is the simplest code. Through that you can directly download your file in excel format.
first you have take one viewstate
To store your data-table Through Which your going to bind Your Griedview1
e.g.,
Griedview1.DataSource=dt;
Griedview1.BindData();
And Then Write This Line;
ViewState["dtData"]=dt
Private Void ExportExcelFile()
{
DataTable dt=new DataTable();
dt=(DataTable)ViewState["dtData"];
GridView gvExp = New GridView();
gvExp.AutoGenerateColumns = False;
int counter = 0;
foreach(DataColumn col In GriedView1.Columns)
{
BoundField bf = New BoundField();
bf.DataField = col.ColumnName;
bf.HeaderText = col.ColumnName;
gvExp.Columns.Add(bf);
counter += 1;
}
gvExp.DataSource = dt;
gvExp.DataBind();
String FileName = ""
FileName = "YourFileName.xls";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" & FileName);
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.Charset = "";
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
System.IO.StringWriter tw = New System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = New System.Web.UI.HtmlTextWriter(tw);
gvExp.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
Hope it helps you.