日期:2014-05-18 浏览次数:20443 次
private void ExportExcel(System.Data.DataTable dt) { System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); System.Web.HttpContext.Current.Response.Buffer = false; System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); System.Web.HttpContext.Current.Response.ClearHeaders(); System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); string sep = ""; foreach (DataColumn dc in dt.Columns) { System.Web.HttpContext.Current.Response.Write(sep + dc.ColumnName); sep = "\t"; } System.Web.HttpContext.Current.Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) { sep = ""; for (i = 0; i < dt.Columns.Count; i++) { string mes = dr[i].ToString(); if (i == 8) { mes = " " + dr[i].ToString(); } System.Web.HttpContext.Current.Response.Write(sep + mes); sep = "\t"; } System.Web.HttpContext.Current.Response.Write("\n"); } System.Web.HttpContext.Current.Response.End(); }