public static void Export(DataTable Dt, string FileName, HttpResponse Response, bool AppendHeader) { StringBuilder sb = new StringBuilder(); int countOfColumns = Dt.Columns.Count; int countOfRows = Dt.Rows.Count; if (AppendHeader) { for (int indexOfCl = 0; indexOfCl < countOfColumns - 1; indexOfCl++) { sb.Append(Dt.Columns[indexOfCl] + "\t"); } if (countOfColumns > 0) { sb.Append(Dt.Columns[countOfColumns - 1] + "\n"); } } for (int indexOfRow = 0; indexOfRow < countOfRows; indexOfRow++) { for (int indexOfCl = 0; indexOfCl < countOfColumns; indexOfCl++) { sb.Append(Dt.Rows[indexOfRow][indexOfCl] + "\t"); } sb.Append("\n"); } Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Convert.ToString(FileName), System.Text.Encoding.UTF8)); Response.Write(sb.ToString()); Response.Flush(); Response.End(); }
var data1 = from f in siteData.已投稿列表 where (BeginTime.Value.Trim() != "" && EndTime.Value.Trim() != "") ? (f.SubmittedDate >= Convert.ToDateTime(BeginTime.Value.Trim())) && f.SubmittedDate <= Convert.ToDateTime(EndTime.Value.Trim()).AddDays(1) : 1 == 1 where f.Department == UserDept group f by f.Department into g where keyword != "" ? g.Key != null ? g.Key.Contains(keyword) : 3 == 3 : 2 == 2 orderby g.Count() descending select new { 类别 = g.Key == null ? "无部门" : g.Key, 文章次数 = g.Count(), 已采纳数 = g.Count(o => o.HandleState == "实际采用") }; if (data1 != null) { DataTable dt = new DataTable(); dt.Columns.Add("部门", typeof(string)); dt.Columns.Add("投稿数量", typeof(string)); dt.Columns.Add("已采纳数", typeof(string)); dt.Rows.Add(new object[] { "部门", "投稿数量", "已采纳数" }); for (int i = 0; i < data1.Count(); i++) { dt.Rows.Add(new object[] { data1.ElementAt(i).类别, data1.ElementAt(i).文章次数, data1.ElementAt(i).已采纳数 }); } CoolFuncs.Export(dt, "部门投稿统计.xls", Response, false); }