Hi,
If Your are using Web App Means
Use this
protected void btnExport_click(object sender, EventArgs e)
{
DataTable dt = objDataLayer.getEmployeeDataTable();
ExporttoExcel(dt,"EmployeeList")
}
private void ExporttoExcel(DataTable table,string filename)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.Write(@");
//HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=InvoiceList.xls");// 2003 format
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ filename +".xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
HttpContext.Current.Response.Write("<font style="font-size:12.0pt; font-family:Calibri;">");
HttpContext.Current.Response.Write("<br><br><br>");
HttpContext.Current.Response.Write("<table border="1" bgcolor="#ffffff" bordercolor="#000000" cellspacing="2px" cellpadding="2px" style="font-size:12.0pt; font-family:Calibri; background:white;"> <tr>");
// int columnscount = grdInvoice.Columns.Count;
int columnscount = table.Columns.Count;
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<td>");
HttpContext.Current.Response.Write("");
// HttpContext.Current.Response.Write(grdInvoice.Columns[j].HeaderText.ToString());
HttpContext.Current.Response.Write(table.Columns[j].ColumnName);
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("</td>");
}
HttpContext.Current.Response.Write("</tr>");
foreach (DataRow row in table.Rows)
{
HttpContext.Current.Response.Write("<tr>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</td>");
}
HttpContext.Current.Response.Write("</tr>");
}
HttpContext.Current.Response.Write("</table>");
HttpContext.Current.Response.Write("</br></br></br></font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
it will write exported excel file to response - Means File being download when you click export