码迷,mamicode.com
首页 > Web开发 > 详细

ExtAspNet从DataTable里导出Excel

时间:2014-09-26 06:29:18      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   strong   for   sp   

  1. protected void btn_ToExcel_Click(object sender, EventArgs e) 
  2.     { 
  3.         Response.ClearContent(); 
  4.         Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); 
  5.         Response.ContentType = "application/excel"
  6.         Response.Write(GetDataTableHtml((DataTable)ViewState["tt"])); 
  7.         Response.End(); 
  8.     } 
  9.  
  10.  
  11. private string GetDataTableHtml(DataTable dt) 
  12.     { 
  13.         StringBuilder sb = new StringBuilder(); 
  14.         sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">"); 
  15.         sb.Append("<tr>"); 
  16.         foreach (DataColumn column in dt.Columns) 
  17.         { 
  18.             sb.AppendFormat("<td>{0}</td>", column.Caption); 
  19.         } 
  20.         sb.Append("</tr>"); 
  21.  
  22.         foreach (DataRow row in dt.Rows) 
  23.         { 
  24.             sb.Append("<tr>"); 
  25.             for (int i = 0; i < dt.Columns.Count;i++ ) 
  26.             { 
  27.                 string html = row[i].ToString(); 
  28.                 sb.AppendFormat("<td>{0}</td>", html); 
  29.             } 
  30.             sb.Append("</tr>"); 
  31.         } 
  32.         sb.Append("</table>"); 
  33.         return sb.ToString(); 
  34.       }

 

 

ExtAspNet从DataTable里导出Excel

标签:style   blog   http   io   os   ar   strong   for   sp   

原文地址:http://blog.csdn.net/laoyi_grace/article/details/39569145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!