Ankit Kumar

Ankit Kumar

  • NA
  • 159
  • 48.3k

Http Context is not Working

Aug 21 2017 4:43 AM
public void ExporttoExcel(DataTable table)
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=EmployeeList.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("");
int columnscount = table.Columns.Count;
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("
");
}
HttpContext.Current.Response.Write("
");
foreach (DataRow row in table.Rows)
{
HttpContext.Current.Response.Write("
");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("
");
}
HttpContext.Current.Response.Write("
");
}
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
}
 
I am using Above Code For Downloading the File.
File is not Download Automatically Response .context is not Working Well.
Please suggest me if there is any one user is online .

Answers (2)