Tech Tips
Would you like to react to this message? Create an account in a few clicks or log in to continue.

C# Export Data Table to Excel

Go down

C# Export Data Table to Excel Empty C# Export Data Table to Excel

Post  Admin Tue Jun 23, 2009 12:07 pm

objCmd = new SqlCommand(vSQLStr, CN);
SqlDataAdapter da = new SqlDataAdapter(objCmd);

DataTable dt = new DataTable();
da.Fill(dt);
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
ExportToExcel("test.xls", dg);
dg = null;
dg.Dispose();

private void ExportToExcel(string strFileName, DataGrid dg)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Admin
Admin

Posts : 60
Join date : 2009-06-05

https://softro.forumotion.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum