private static ExcelCategoryHandler ParseCategoryHandler(HttpContext context) { string url = context.Request.Path; ExcelOutputEnum outputType = ParseUrlSuffix(url); //必须是.jx结尾 if (outputType == ExcelOutputEnum.Unknown) { throw new ApplicationException("不是有效的Excel文件类型。 url=" + url); } string[] parts = GetUrlDivision(context); if (parts.Length == 0) { throw new ApplicationException("未指定Excel文档类型。 url=" + url); //无Controller } ExcelCategoryHandler controller = ExcelCategoryHandler.GetCategoryHandler(parts[0]); // parts[0] should be controller name if (controller == null) { throw new ApplicationException("未找到Excel文档的处理对象。 url=" + url); //无Controller } return(controller); }
private static void InitExcelHandlers() { // 指定的多个带有controller的assembly的名称 string handlerAssemblies = AppSettingConfig.GetSetting("ExcelHandlerAssemblies", ""); //"LJTH.HR.KPI.Web.Common"; _excelHandlers = new Dictionary <string, ExcelCategoryHandler>(); string[] assemblyArray = handlerAssemblies.Split(",;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); List <Type> handlerTypes = new List <Type>(); foreach (var assembly in assemblyArray) { var types = (from t in Assembly.Load(assembly).GetTypes() where IsSubClassOf(t, typeof(ExcelCategoryHandler)) select t).ToList(); handlerTypes.AddRange(types); } foreach (Type type in handlerTypes) { string typeName = type.Name.ToLower(); // todo alias ExcelCategoryHandler handler = (ExcelCategoryHandler)Activator.CreateInstance(type); _excelHandlers.Add(typeName, handler); } }
internal static void GenerateExcel(HttpContext context) { ExceptionHelper.FalseThrow <ArgumentNullException>(context != null, "HttpContext"); ExcelCategoryHandler categoryHandler = ParseCategoryHandler(context); // throw ContrllerParseFailException or ControllerNotFoundException if (categoryHandler.CheckBeforeDownload(context)) { Object queryResult = categoryHandler.Query(context); if (categoryHandler.OutputType == ExcelOutputEnum.FileStream) { MemoryStream stream = categoryHandler.ToStream(queryResult); if (stream == null) { context.Response.Write("无内容。"); context.Response.End(); } else { context.Response.Clear(); context.Response.Buffer = true; context.Response.Charset = "utf-8"; context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(categoryHandler.FileName, System.Text.Encoding.UTF8) + ".xls"); context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.ContentType = "application/ms-excel"; context.Response.BinaryWrite(stream.ToArray()); context.Response.End(); } } else if (categoryHandler.OutputType == ExcelOutputEnum.HtmlPage) { // 返回显示, 调试用 context.Items["queryResult"] = queryResult; context.Server.Execute("~/_pagelet/excelhtml.aspx"); } } else { HttpContext.Current.Response.Clear(); context.Response.Write("未通过检查!请与系统管理员联系!"); context.Response.End(); } }