public void GenerateExcel()
{
System.Data.DataTable dt = new
System.Data.DataTable();
dt.Columns.Add("AccountNo");
for (int i = 0; i < 10; i++)
{
string a = "00001234456" + i;
if (a.StartsWith("0"))
{
a =
a.Substring(1).Replace("0", "&CHAR(048)&");
a =
"=CHAR(048)" + a;
if (a.EndsWith("&"))
a = a.Substring(0, a.Length - 1);
a =
a.Replace("&&", "&");
}
dt.Rows.Add(a);
}
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
string FileName = "Excel";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" + FileName + ".xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
//Open a memory stream that you can
use to write back to the response
byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
MemoryStream s = new MemoryStream(byteArray);
StreamReader sr = new StreamReader(s, Encoding.ASCII);
HttpContext.Current.Response.Write(sr.ReadToEnd());
HttpContext.Current.Response.Flush(); // Sends all currently buffered
output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether
to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest();
}