using System; using System.Collections.Generic; using System.Net; using Neo.Afx.ComponentModel; using System.Linq; using Neo.Afx.Services.Reports; using DevExpress.XtraReports.UI; using System.IO; namespace Neo.Afx.Services.Reports { /// /// Report generator static class /// public static class ReportGenerator { #region Constants /// /// CSV file format type for this report mime type /// public const string FORMAT_CSV = "CSV"; /// /// EXCEL file format type for this report mime type /// public const string FORMAT_EXCEL = "EXCEL"; /// /// PDF file format type for this report mime type /// public const string FORMAT_PDF = "PDF"; #endregion #region GetMimeTypeForFormat /// /// Gets the MIME type for format. /// /// The format. /// public static string GetMimeTypeForFormat(string format) { switch(format) { case FORMAT_CSV: return "text/csv"; case FORMAT_EXCEL: return "application/msexcel"; case FORMAT_PDF: return "application/pdf"; } return "application/octet-stream"; } #endregion #region RenderPdf /// /// Renders the given report as a PDF. /// /// /// When this method returns, contains the error message if an error occurred. /// The report parameters. /// The content of the report or null if an error occurred. public static byte[] RenderPdf(ReportDefinition reportDefinition, List> reportParameters, out string error) { return Render(reportDefinition, FORMAT_PDF, reportParameters, out error); } #endregion #region RenderXL /// /// Renders the given report as a EXCEL. /// /// /// When this method returns, contains the error message if an error occurred. /// The report parameters. /// The content of the report or null if an error occurred. public static byte[] RenderXl(ReportDefinition reportDefinition, List> reportParameters, out string error) { return Render(reportDefinition, FORMAT_EXCEL, reportParameters, out error); } #endregion #region Render /// /// Renders the given report in the specified format. /// /// /// The format in which the report is to be rendered. /// When this method returns, contains the error message if an error occurred. /// The report parameters. /// The content of the report or null if an error occurred. public static byte[] Render(ReportDefinition reportDefinition, string format, List> reportParameters, out string error) { error = null; try { var assembly = reportDefinition.Server.Url; var typename = assembly + "." + reportDefinition.ReportFolder + "." + reportDefinition.ReportName; var temp = Type.GetType(typename + ", " + assembly); var report = (XtraReport)Activator.CreateInstance(Type.GetType(typename + ", " + assembly)); foreach (var p in reportParameters) { report.Parameters[p.Key].Value = p.Value; } using (MemoryStream localStream = new MemoryStream()) { if (format == FORMAT_EXCEL) { report.ExportToXlsx(localStream); } else if (format == FORMAT_PDF) { report.ExportToPdf(localStream); } else if (format == FORMAT_CSV) { report.ExportToCsv(localStream); } else { throw new Exception("Invalid report format specified:" + format); } return localStream.ToArray(); } } catch (Exception ex) { error = (ex.Message.IndexOf("Inner Exception") != -1 ? ex.InnerException.Message : ex.Message); } return null; } #endregion } #region Report /// /// A report object. /// public class Report : FileObject { /// /// Initializes a new instance of the class. /// public Report() { } /// /// Determines whether the report has content. /// public bool HasContent { get { return Content != null; } } /// /// Gets the content length. /// public new long Length { get { return Content == null ? 0 : Content.LongLength; } } /// /// The error (if any) that occurred during report generation /// public string Error; /// /// Creates a report with the given name, extension and mime type. /// /// The name of the report. /// The extension of the report. /// The mime type of the report. /// A new report object. public static Report Create(string name, string extension, string mimeType) { return new Report { Name = name, Extension = extension, MimeType = mimeType }; } } #endregion }