C# Export Data Table to Excel

View previous topic View next topic Go down

C# Export Data Table to Excel

Post  Admin on 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 : 50
Join date : 2009-06-05

View user profile http://softro.forumotion.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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