mvc 导出

  int teacherId = int.Parse(CurrentContext.CurrentUser.UserId);
            string _courseName = Request["ddlCourse"] == "全部" ? string.Empty : Request["ddlCourse"];
            List<ClassModel> list = _classBll.GetClassList(int.Parse(Request["selTerm"]), int.Parse(Request["ddlClass"]), teacherId, _courseName);
            var sbHtml = new StringBuilder();
        
            sbHtml.Append("<meta http-equiv='content-type' content='application/ms-excel; charset=utf-8'/>");
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "学时", "上课时间", "教学班级名称", "班级人数", "上课人数", "缺勤人数", "请假人数" };
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");
            for (int i = 0; i < list.Count; i++)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].TeachingHours);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].Time);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].TeachClassName);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].TotalCount);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].NormalCount);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].AbsenceCount);
                sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].LeaveCount);
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");
         

            byte[] fileContents =  Encoding.UTF8.GetBytes(sbHtml.ToString());
            var fileStream = new System.IO.MemoryStream(fileContents);
            return File(fileStream, "application/ms-excel", "班级上课信息.xls");

posted on 2015-12-08 17:16  Allen_T  阅读(90)  评论(0)    收藏  举报

导航