日期:2014-05-17 浏览次数:20658 次
public ActionResult ExportQualifiedExaminees(int serviceid, int funcid, string condition) { RegistrationBLL bll = new RegistrationBLL(); IList<QualifiedExamineeEn> list = bll.GetQualifiedExaminees(serviceid, condition); if (list == null || list.Count == 0) { return Alert("没有准考证信息", "~/Views/ExamService/SaveSuccess.aspx", new { controller = "Registration", action = "GetExamineeByPage", serviceid = serviceid, funcid = funcid }); } using (MemoryStream m = bll.ExportQualifiedExaminees(Server.MapPath("~/Resources/考生签到表导出模版.xls"), list1[0].fServiceName, list, Server.MapPath("~/Common/Images/toeic_log.PNG"))) { ExcelExportHandler.ExportFile(m, "application/ms-excel", "UTF-8", "GB2312", "考生签到表.xls"); } return new EmptyResult(); }
------解决方案--------------------
ExcelExportHandler类的定义:
public class ExcelExportHandler { public static void ExportFile(string content, string contentType, string charSet, string encodingName, string outPutFileName) { byte[] htmlBy = System.Text.Encoding.GetEncoding("GB2312").GetBytes(content); MemoryStream stream = new MemoryStream(htmlBy); ExportFile(stream, contentType, charSet, encodingName, outPutFileName); } public static void ExportFile(MemoryStream stream, string contentType, string charSet, string encodingName, string outPutFileName) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = contentType; HttpContext.Current.Response.Charset = charSet; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(encodingName); String userAgent; userAgent = HttpContext.Current.Request.UserAgent; if (userAgent != null && userAgent.ToUpper().IndexOf("MSIE") > -1) { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(outPutFileName, System.Text.Encoding.UTF8).Replace("+", "%20")); // The browser is Microsoft Internet Explorer Version 6.0. } else if (userAgent != null && userAgent.ToUpper().IndexOf("MOZILLA") > -1) { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(outPutFileName, System.Text.Encoding.UTF8).Replace("+", "%20")); } HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding(encodingName); stream.WriteTo(HttpContext.Current.Response.OutputStream); //HttpContext.Current.Response.End(); } }