C#

Export Datatable To excel .cvs File with c#

DataTable dtUsers = Session[“UsersForApproval”] as DataTable;

DataTable dtexprot = new DataTable();
if (Session[“UsersForApproval”] == null)
{
dtexprot = UserDetails.GetUsersForApproval();
Session[“UsersForApproval”] = dtUsers;
}
else
{
dtexprot = Session[“UsersForApproval”] as DataTable;
}
HttpContext context = HttpContext.Current;

context.Response.Clear();

foreach (DataColumn column in dtexprot.Columns)
{

context.Response.Write(column.ColumnName + “,”);

}

context.Response.Write(Environment.NewLine);

foreach (DataRow row in dtexprot.Rows)
{

for (int i = 0; i < dtexprot.Columns.Count; i++)
{

context.Response.Write(row[i].ToString().Replace(“,”, string.Empty) + “,”);

}

context.Response.Write(Environment.NewLine);

}

context.Response.ContentType = “text/csv”;

context.Response.AppendHeader(“Content-Disposition”, “attachment; filename=” + “Excel” + “.csv”);

context.Response.End();

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.