Exemplo n.º 1
0
        public async Task <JsonResult> ViewReportPage(string reportPath, int?page = 0)
        {
            var model = this.GetReportViewerModel(Request);

            model.ViewMode   = ReportViewModes.View;
            model.ReportPath = reportPath;

            var contentData = await ReportServiceHelpers.ExportReportToFormatAsync(model, ReportFormats.Html4_0, page, page);

            var content = model.Encoding.GetString(contentData.ReportData);

            if (model.UseCustomReportImagePath && model.ReportImagePath.HasValue())
            {
                content = ReportServiceHelpers.ReplaceImageUrls(model, content);
            }

            var jsonResult = Json(
                new
            {
                CurrentPage = contentData.CurrentPage,
                Content     = content,
                TotalPages  = contentData.TotalPages
            }
                );

            return(jsonResult);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> PrintReport(string reportPath)
        {
            var model = this.GetReportViewerModel(Request);

            model.ViewMode   = ReportViewModes.Print;
            model.ReportPath = reportPath;

            var contentData = await ReportServiceHelpers.ExportReportToFormatAsync(model, ReportFormats.Html4_0);

            var content = model.Encoding.GetString(contentData.ReportData);

            content = ReportServiceHelpers.ReplaceImageUrls(model, content);

            var sb = new System.Text.StringBuilder();

            sb.AppendLine("<html>");
            sb.AppendLine("	<body>");
            //sb.AppendLine($"		<img src='data:image/tiff;base64,{Convert.ToBase64String(contentData.ReportData)}' />");
            sb.AppendLine($"		{content}");
            sb.AppendLine("		<script type='text/javascript'>");
            sb.AppendLine("			(function() {");

            /*
             * sb.AppendLine("				var beforePrint = function() {");
             * sb.AppendLine("					console.log('Functionality to run before printing.');");
             * sb.AppendLine("				};");
             */
            sb.AppendLine("				var afterPrint = function() {");
            sb.AppendLine("					window.onfocus = function() { window.close(); };");
            sb.AppendLine("					window.onmousemove = function() { window.close(); };");
            sb.AppendLine("				};");

            sb.AppendLine("				if (window.matchMedia) {");
            sb.AppendLine("					var mediaQueryList = window.matchMedia('print');");
            sb.AppendLine("					mediaQueryList.addListener(function(mql) {");
            sb.AppendLine("						if (mql.matches) {");
            //sb.AppendLine("							beforePrint();");
            sb.AppendLine("						} else {");
            sb.AppendLine("							afterPrint();");
            sb.AppendLine("						}");
            sb.AppendLine("					});");
            sb.AppendLine("				}");

            //sb.AppendLine("				window.onbeforeprint = beforePrint;");
            sb.AppendLine("				window.onafterprint = afterPrint;");

            sb.AppendLine("			}());");
            sb.AppendLine("			window.print();");
            sb.AppendLine("		</script>");
            sb.AppendLine("	</body>");

            sb.AppendLine("<html>");

            return(Content(sb.ToString(), "text/html"));
        }
Exemplo n.º 3
0
        public async Task <FileResult> ExportReport(string reportPath, string format)
        {
            var model = this.GetReportViewerModel(Request);

            model.ViewMode   = ReportViewModes.Export;
            model.ReportPath = reportPath;

            var extension = "";

            switch (format.ToUpper())
            {
            case "CSV":
                format    = "CSV";
                extension = ".csv";
                break;

            case "MHTML":
                format    = "MHTML";
                extension = ".mht";
                break;

            case "PDF":
                format    = "PDF";
                extension = ".pdf";
                break;

            case "TIFF":
                format    = "IMAGE";
                extension = ".tif";
                break;

            case "XML":
                format    = "XML";
                extension = ".xml";
                break;

            case "WORDOPENXML":
                format    = "WORDOPENXML";
                extension = ".docx";
                break;

            case "EXCELOPENXML":
            default:
                format    = "EXCELOPENXML";
                extension = ".xlsx";
                break;
            }

            var contentData = await ReportServiceHelpers.ExportReportToFormatAsync(model, format);

            var filename = reportPath;

            if (filename.Contains("/"))
            {
                filename = filename.Substring(filename.LastIndexOf("/"));
                filename = filename.Replace("/", "");
            }

            filename = filename + extension;

            return(File(contentData.ReportData, contentData.MimeType, filename));
        }
Exemplo n.º 4
0
        public static async Task <HtmlString> RenderReportViewerAsync(this IHtmlHelper helper, ReportViewerModel model, int?startPage = 1)
        {
            var sb = new StringBuilder();

            var reportServerDomainUri = new Uri(model.ServerUrl);
            var contentData           = await ReportServiceHelpers.ExportReportToFormatAsync(model, ReportFormats.Html4_0, startPage, startPage);

            sb.AppendLine("<form class='form-inline' id='frmReportViewer' name='frmReportViewer'>");
            sb.AppendLine("	<div class='ReportViewer row'>");
            sb.AppendLine("		<div class='ReportViewerHeader row'>");
            sb.AppendLine("			<div class='ParametersContainer col-sm-12'>");
            sb.AppendLine("				<div class='Parameters col-sm-10'>");

            sb.AppendLine(await ParametersToHtmlString(contentData.Parameters, model));

            sb.AppendLine("				</div>");

            sb.AppendLine("				<div class='ReportViewerViewReport col-sm-2 text-center'>");
            sb.AppendLine("					<button type='button' class='btn btn-primary ViewReport'>View Report</button>");
            sb.AppendLine("				</div>");
            sb.AppendLine("			</div>");

            sb.AppendLine("			<div class='ReportViewerToolbar row'>");
            sb.AppendLine("				<div class='ReportViewerPager'>");
            sb.AppendLine("					<div class='btn-toolbar'>");

            if (model.EnablePaging)
            {
                sb.AppendLine("						<div class='btn-group'>");
                sb.AppendLine($"							<a href='#' title='First Page' class='btn btn-default FirstPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-backward'></span></a>");
                sb.AppendLine($"							<a href='#' title='Previous Page' class='btn btn-default PreviousPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-left'></span></a>");
                sb.AppendLine("						</div>");
                sb.AppendLine("						<div class='btn-group'>");
                sb.AppendLine($"							<span class='PagerNumbers'><input type='text' id='ReportViewerCurrentPage' name='ReportViewerCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> of <span id='ReportViewerTotalPages'>{contentData.TotalPages}</span></span>");
                sb.AppendLine("						</div>");
                sb.AppendLine("						<div class='btn-group'>");
                sb.AppendLine($"							<a href='#' title='Next Page' class='btn btn-default NextPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-right'></span></a>");
                sb.AppendLine($"							<a href='#' title='Last Page' class='btn btn-default LastPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-forward'></span></a>");
                sb.AppendLine("						</div>");
            }
            sb.AppendLine("						<div class='btn-group'>");
            sb.AppendLine("							<span class='SearchText'>");
            sb.AppendLine($"								<input type='text' id='ReportViewerSearchText' name='ReportViewerSearchText' class='form-control' value='' />");
            sb.AppendLine($"								<a href='#' title='Find' class='btn btn-info FindTextButton'><span class='glyphicon glyphicon-search' style='padding-right: .5em;'></span>Find</a>");
            sb.AppendLine("							</span>");
            sb.AppendLine("						</div>");
            sb.AppendLine("						<div class='btn-group'>");
            sb.AppendLine("							<a href='#' title='Export' class='dropdown-toggle btn btn-default' data-toggle='dropdown' role='button' aria-haspopup='true' area-expanded='false'>");
            sb.AppendLine("								<span class='glyphicon glyphicon-floppy-save' style='color: steelblue;'></span>");
            sb.AppendLine("								<span class='caret'></span>");
            sb.AppendLine("							</a>");
            sb.AppendLine("							<ul class='dropdown-menu'>");
            sb.AppendLine("								<li><a href='#' class='ExportCsv'>CSV (comma delimited)</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportExcelOpenXml'>Excel</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportMhtml'>MHTML (web archive)</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportPdf'>PDF</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportTiff'>TIFF file</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportWordOpenXml'>Word</a></li>");
            sb.AppendLine("								<li><a href='#' class='ExportXml'>XML file with report data</a></li>");
            sb.AppendLine("							</ul>");
            //sb.AppendLine("						</div>");
            //sb.AppendLine("						<div class='btn-group'>");
            sb.AppendLine("							<a href='#' title='Refresh' class='btn btn-default Refresh'><span class='glyphicon glyphicon-refresh' style='color: green;'></span></a>");
            //sb.AppendLine("						</div>");
            //sb.AppendLine("						<div class='btn-group'>");
            sb.AppendLine("							<a href='#' title='Print' class='btn btn-default Print'><span class='glyphicon glyphicon-print' style='color: grey;'></span></a>");
            sb.AppendLine("						</div>");
            sb.AppendLine("					</div>");
            sb.AppendLine("				</div>");
            sb.AppendLine("			</div>");
            sb.AppendLine("		</div>");
            sb.AppendLine("		<div class='ReportViewerContentContainer row'>");
            sb.AppendLine("			<div class='ReportViewerContent'>");

            if (model.IsMissingAnyRequiredParameterValues(contentData.Parameters))
            {
                sb.AppendLine("			<div class='ReportViewerInformation'>Please fill parameters and run the report...</div>");
            }
            else
            {
                if (model.AjaxLoadInitialReport)
                {
                    sb.AppendLine("			<script type='text/javascript'>$(document).ready(function () { viewReportPage(1); });</script>");
                }
                else
                {
                    if (contentData == null || contentData.ReportData == null || contentData.ReportData.Length == 0)
                    {
                        sb.AppendLine("");
                    }
                    else
                    {
                        var content = model.Encoding.GetString(contentData.ReportData);

                        if (model.UseCustomReportImagePath && model.ReportImagePath.HasValue())
                        {
                            content = ReportServiceHelpers.ReplaceImageUrls(model, content);
                        }

                        sb.AppendLine($"			{content}");
                    }
                }
            }

            sb.AppendLine("			</div>");
            sb.AppendLine("		</div>");
            sb.AppendLine("	</div>");
            sb.AppendLine("</form>");

            sb.AppendLine("<script type='text/javascript'>");
            sb.AppendLine("	function ReportViewer_Register_OnChanges() {");

            var dependencyFieldKeys = new List <string>();

            foreach (var parameter in contentData.Parameters.Where(x => x.Dependencies != null && x.Dependencies.Any()))
            {
                foreach (var key in parameter.Dependencies)
                {
                    if (!dependencyFieldKeys.Contains(key))
                    {
                        dependencyFieldKeys.Add(key);
                    }
                }
            }

            foreach (var queryParameter in contentData.Parameters.Where(x => dependencyFieldKeys.Contains(x.Name)))
            {
                sb.AppendLine("		$('#"+ queryParameter.Name + "').change(function () {");
                sb.AppendLine("			reloadParameters();");
                sb.AppendLine("		});");
            }

            sb.AppendLine("	}");

            sb.AppendLine("</script>");

            return(new HtmlString(sb.ToString()));
        }