Thursday, May 3, 2012

Export DataTable to Excel File in asp.net

dt = city.GetAllCity();//your datatable 
string attachment = "attachment; filename=city.xls"; 
 Response.ClearContent(); Response.AddHeader("content-disposition", attachment);
  Response.ContentType = "application/vnd.ms-excel"; string tab = ""; 
 foreach (DataColumn dc in dt.Columns) { 
   
Response.Write(tab + dc.ColumnName);
    tab
= "\t"; } Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) {
    tab
= "";
   
for (i = 0; i < dt.Columns.Count; i++)
   
{
       
Response.Write(tab + dr[i].ToString());
        tab
= "\t";
   
}
   
Response.Write("\n"); } Response.End(); 
 
 
http://stackoverflow.com/questions/7843822/export-datatable-to-excel-file-in-asp-net 

No comments:

Post a Comment