Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [OutputCache(Location = OutputCacheLocation.Client, Duration = 0, VaryByParam = "id")]
- [Dependency(typeof(ReportDialogScriptBuilder))]
- public ActionResult Run()
- {
- ActionResult result = null;
- // parse the content type
- ReportContentType reportContentType = GetRequestValue(ReportFilterType.ReportContentType).ToEnumeration<ReportContentType>();
- // parse the report id
- int reportId = GetRequestValue(ReportFilterType.ReportType).ToInt32();
- // declare file extension
- string extension = null;
- ContentType contentType = ContentType.HTML;
- // determine the content type
- switch (reportContentType)
- {
- case ReportContentType.PDF:
- contentType = ContentType.PDF;
- extension = "pdf";
- break;
- case ReportContentType.Excel:
- contentType = ContentType.XLS;
- extension = "xls";
- break;
- case ReportContentType.Word:
- contentType = ContentType.DOC;
- extension = "doc";
- break;
- case ReportContentType.HTML:
- default:
- break;
- }
- var reportData = this.DataContextProvider.Reporting.GetReport(reportId).SingleOrDefault();
- if (null == reportData)
- throw new SystemException("The specified report does not exist.");
- RunModes runModes = (RunModes)reportData.RunMode;
- // check whether this report uses a custom HTML report writer
- if (runModes.HasFlag(RunModes.Writer))
- {
- StringBuilder sb = new StringBuilder();
- var sections = DataContextProvider.Reporting.GetReportSections(reportID: reportId);
- foreach (var section in sections)
- {
- // ASSUMPTION: When multiple sections are defined, the individual writers will be implemented to open / close tags correctly (e.g. <HTML>, <BODY>, etc.)
- if (!String.IsNullOrEmpty(section.Header))
- {
- sb.AppendLine("<h2>" + TextBlock.Create(section.Header) + "</h2>");
- }
- // get the IHtmlMarkupWriter / Writer-derived formatter type name
- string typeName = section.FormatterType;
- Type type = Type.GetType(typeName);
- if (null != type)
- {
- IHtmlMarkupWriter writer = Activator.CreateInstance(type) as IHtmlMarkupWriter;
- if (null != writer)
- {
- var properties = type.GetProperties();
- foreach (var property in properties)
- {
- var attribs = Reflection.ReflectionUtility.GetAttributesOfType<Reporting.ReportFilterTypeAttribute>(property);
- foreach (var attrib in attribs)
- {
- if (null != attrib)
- {
- string value = Resolver.GetReportFilter(attrib.Type);
- object raw = ConversionUtility.ChangeType(value, property.PropertyType);
- property.SetValue(writer, raw, null);
- // break on the first non-null value that is set
- if (null != raw) break;
- }
- }
- }
- sb.AppendLine(writer.Render().ToHtmlString());
- }
- }
- }
- switch (contentType)
- {
- case ContentType.DOC:
- FileContentResult fcr = File(fileContents: Encoding.UTF8.GetBytes(sb.ToString()),
- contentType: contentType.ToAbbreviation<ContentType>(),
- fileDownloadName: reportData.Name + ".doc");
- result = fcr;
- break;
- case ContentType.HTML:
- default:
- ContentResult cr = new ContentResult();
- cr.Content = sb.ToString();
- cr.ContentType = contentType.ToAbbreviation<ContentType>();
- cr.ContentEncoding = System.Text.Encoding.UTF8;
- result = cr;
- break;
- }
- }
- // check to see if rendering directly to file or to web control
- else if (String.IsNullOrEmpty(extension))
- {
- // render using ReportViewer as HTML
- string url = "/Reports/Viewer.aspx?" + Request.QueryString.ToString();
- result = new RedirectResult(url);
- }
- else
- {
- // render directly to specified content type
- try
- {
- ReportHelper helper = new ReportHelper(reportId, Resolver);
- byte[] renderedBytes = helper.Render(reportContentType,
- height: reportData.Height,
- width: reportData.Width,
- marginBottom: reportData.BottomMargin,
- marginLeft: reportData.LeftMargin,
- marginRight: reportData.RightMargin,
- marginTop: reportData.TopMargin);
- // check to see if a user-specified filename should be used
- string name = GetRequestValue(ReportFilterType.ReportFileName);
- // check the name value
- if (String.IsNullOrEmpty(name))
- {
- // get the default download name based on the report type
- name = ReportUtility.GetDownloadName(reportId);
- }
- // format the download file name
- string fileName = String.Format("{0}.{1}", name, extension);
- result = File(renderedBytes, helper.ContentType, fileName);
- }
- catch (Exception ex)
- {
- HandleException(ex);
- result = Error();
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement