public ActionResult PrintReport(string reportPath, HttpRequest request) { var model = this.GetReportViewerModel(request); model.ViewMode = ReportViewModes.Print; model.ReportPath = reportPath; var contentData = ReportServiceHelpers.ExportReportToFormat(model, ReportFormats.HTML5); 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($" {content}"); sb.AppendLine(" <script type='text/javascript'>"); sb.AppendLine(" (function() {"); sb.AppendLine(" var beforePrint = function() {"); sb.AppendLine(" console.log('Before print ...');"); sb.AppendLine(" };"); sb.AppendLine(" var afterPrint = function() {"); sb.AppendLine(" console.log('Apter print.');"); //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"); return(new ContentResult() { Content = sb.ToString(), ContentType = "text/html", StatusCode = 200 }); }
public JsonResult FindStringInReport(string reportPath, string searchText, HttpRequest request, int?page = 0) { var model = this.GetReportViewerModel(request); model.ViewMode = ReportViewModes.View; model.ReportPath = reportPath; //return Json(ReportServiceHelpers.FindStringInReport(model, searchText, page).ToInt32()); return(new JsonResult(ReportServiceHelpers.FindStringInReport(model, searchText, page).ToInt32())); }
public string GetPathRender(bool showParameters = true, bool showToolbar = true, bool addTagEmbed = true) { string path = ReportServiceHelpers.AddCharacterStartEnd(ServerUrl, false, true, "/") + "Pages/ReportViewer.aspx?" + ReportPath + "&rs:Command=Render" + (addTagEmbed ? "&rs:embed=true" : ""); if (!showParameters) { path += "&rc:Parameters=false"; } if (!showToolbar) { path += "&rc:Toolbar=false"; } return(path); }
public static HtmlString RenderScriptReportViewer(this IHtmlHelper helper, ReportViewerModel model) { StringBuilder sb = new StringBuilder(); var contentData = ReportServiceHelpers.ExportReportToFormat(model, ReportFormats.HTML5, 1, 1); if (model.AjaxLoadInitialReport) { sb.AppendLine("<script type='text/javascript'>$(document).ready(function () { viewReportPage(1); });</script>"); } 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>"); string s = sb.ToString(); return(new HtmlString(sb.ToString())); }
public FileResult ExportReport(string reportPath, string format, HttpRequest request) { var model = this.GetReportViewerModel(request); model.ViewMode = ReportViewModes.Export; model.ReportPath = reportPath; string 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 "PPTX": format = "PPTX"; extension = ".pptx"; 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 = ReportServiceHelpers.ExportReportToFormat(model, format); string filename = reportPath; if (filename.Contains("/")) { filename = filename.Substring(filename.LastIndexOf("/")); filename = filename.Replace("/", ""); } filename += extension; //return File(contentData.ReportData, contentData.MimeType, filename); return(new FileContentResult(contentData.ReportData, contentData.MimeType) { FileDownloadName = filename }); }
public JsonResult ViewReportPage(HttpRequest request, string reportPath, int?page = 0) //public string ViewReportPage(HttpRequest request, string reportPath, int? page = 0) { try { ReportViewerModel model = this.GetReportViewerModel(request); model.ViewMode = ReportViewModes.View; model.ReportPath = reportPath; var contentData = ReportServiceHelpers.ExportReportToFormat(model, ReportFormats.HTML5, page, page); var content = model.Encoding.GetString(contentData.ReportData); if (model.UseCustomReportImagePath && model.ReportImagePath.HasValue()) { content = ReportServiceHelpers.ReplaceImageUrls(model, content); } var sb = new System.Text.StringBuilder(); sb.AppendLine("<div>"); sb.AppendLine($"{content}"); sb.AppendLine("</div>"); //var settings = new Newtonsoft.Json.JsonSerializerSettings //{ // //ContractResolver = new CamelCasePropertyNamesContractResolver() // ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver() //}; JsonResult jsonR = new JsonResult(new { CurrentPage = contentData.CurrentPage, Content = sb.ToString(), TotalPages = contentData.TotalPages } ) { //ContentType = "text/html; charset=utf-8", ContentType = "application/json; charset=utf-8", StatusCode = 200, //SerializerSettings = settings }; return(jsonR); //return sb.ToString(); /* * var json = Newtonsoft.Json.JsonConvert.SerializeObject(new ReportContentResult(contentData.CurrentPage, contentData.TotalPages, content)); * return json; * * JsonResult jsonResult = new JsonResult( * Newtonsoft.Json.JsonConvert.SerializeObject(new ReportContentResult(contentData.CurrentPage, contentData.TotalPages, content)) * , new Newtonsoft.Json.JsonSerializerSettings() * { * ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver() * }); * * JsonResult jsonResult2 = new JsonResult( * new * { * CurrentPage = contentData.CurrentPage, * Content = content, * TotalPages = contentData.TotalPages * } * , new Newtonsoft.Json.JsonSerializerSettings() * { * ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver() * }); * * return jsonResult; */ } catch (Exception ex) { //return new JsonResult(new { Status = 500, statusText = ex.Message }); throw; } }
public static HtmlString RenderReportViewer_Borrar(this IHtmlHelper helper, ReportViewerModel model, int?startPage = 1) { string tagsParameters; StringBuilder sb = new StringBuilder(); var contentData = ReportServiceHelpers.ExportReportToFormat(model, ReportFormats.HTML5, startPage, startPage); sb.AppendLine("<form class='d-flex form-inline' id='frmReportViewer' name='frmReportViewer'>"); sb.AppendLine(" <div class='d-flex flex-column align-items-start'>"); tagsParameters = ParametersToHtmlString(contentData.Parameters, model); if (!string.IsNullOrWhiteSpace(tagsParameters)) { sb.AppendLine(tagsParameters); } sb.AppendLine(" <div class='container-fluid reportToolbar'>"); sb.AppendLine(" <div class='btn-toolbar' role='toolbar'>"); if (model.EnablePaging) { sb.AppendLine(" <div class='btn-group btn-group-sm reportToolbarGroup' role='group'>"); sb.AppendLine(" <div class='btn-group btn-group-sm' role='group'>"); sb.AppendLine($" <button name='ReportFirstPage' id='ReportFirstPage' title='Primera página' type='button' class='btn btn-light'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><i class='fa fa-step-backward'></i></button>"); sb.AppendLine($" <button name='ReportPreviousPage' id='ReportPreviousPage' title='Página anterior' type='button' class='btn btn-light'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><i class='fa fa-chevron-left'></i></button>"); sb.AppendLine(" </div>"); sb.AppendLine(" <div class='input-group'>"); sb.AppendLine($" <span class='pagerNumbers'><input type='text' id='ReportCurrentPage' name='ReportCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> de <span id='ReportTotalPages'>{contentData.TotalPages}</span></span>"); sb.AppendLine(" </div>"); sb.AppendLine(" <div class='btn-group btn-group-sm mr-2' role='group'>"); sb.AppendLine($" <button name='ReportNextPage' id='ReportNextPage' title='Página siguiente' type='button' class='btn btn-light'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><i class='fa fa-chevron-right'></i></button>"); sb.AppendLine($" <button name='ReportLastPage' id='ReportLastPage' title='Última página' type='button' class='btn btn-light'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><i class='fa fa-step-forward'></i></button>"); sb.AppendLine(" </div>"); sb.AppendLine(" </div>"); } sb.AppendLine(" <div class='btn-group btn-group-sm mr-2 reportToolbarGroup' role='group'>"); sb.AppendLine(" <a href='#' name='ReportRefresh' id='ReportRefresh' title='Actualizar' class='btn btn-light'><span style='color: green;'><i class='fa fa-refresh'></i></span></a>"); sb.AppendLine(" "); sb.AppendLine(" <div class='btn-group' role='group'>"); sb.AppendLine(" <button id='ReportExport' name='ReportExport' title='Exportar' type='button' class='btn btn-light dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>"); sb.AppendLine(" <span style='color: steelblue;'>Exportar <i class='fa fa-save'></i></span>"); sb.AppendLine(" </button>"); sb.AppendLine(" <div class='dropdown-menu' aria-labelledby='btnSave'>"); sb.AppendLine(" <a name='ReportExportWordOpenXml' id='ReportExportWordOpenXml' class='dropdown-item' href='#'>Word</a>"); sb.AppendLine(" <a name='ReportExportExcelOpenXml' id='ReportExportExcelOpenXml' class='dropdown-item' href='#'>Excel</a>"); sb.AppendLine(" <a name='ReportExportPPTX' id='ReportExportPPTX' class='dropdown-item' href='#'>PowerPoint</a>"); sb.AppendLine(" <a name='ReportExportPdf' id='ReportExportPdf' class='dropdown-item' href='#'>PDF</a>"); sb.AppendLine(" <a name='ReportExportTiff' id='ReportExportTiff' class='dropdown-item' href='#'>Archivo TIFF</a>"); sb.AppendLine(" <a name='ReportExportMhtml' id='ReportExportMhtml' class='dropdown-item' href='#'>MHTML (archivo web)</a>"); sb.AppendLine(" <a name='ReportExportCsv' id='ReportExportCsv' class='dropdown-item' href='#'>CSV (delimitado por coma)</a>"); sb.AppendLine(" <a name='ReportExportXml' id='ReportExportXml' class='dropdown-item' href='#'>XML con datos del informe</a>"); sb.AppendLine(" </div>"); sb.AppendLine(" </div>"); sb.AppendLine(" "); sb.AppendLine(" <a name='ReportPrint' id='ReportPrint' href='#' title='Imprimir' class='btn btn-light'><i class='fa fa-print'></i></a>"); sb.AppendLine(" </div>"); sb.AppendLine(" <div class='input-group mr-2 reportToolbarGroup' role='group'"); sb.AppendLine(" <span class='searchText'>"); sb.AppendLine($" <input type='text' id='ReportSearchText' name='ReportSearchText' class='form-control SearchText' value='' />"); sb.AppendLine($" <a name='ReportFind' id='ReportFind' href='#' title='Buscar' class='btn btn-light'><span style='padding-right: .5em;'>Buscar <i class='fa fa-search'></i></span></a>"); sb.AppendLine(" </span>"); sb.AppendLine(" </div>"); sb.AppendLine(" <div class='btn-group btn-group-sm reportToolbarGroup' role='group'>"); sb.AppendLine(" <button name='ReportShow' id='ReportShow' type='button' class='btn btn-sm btn-primary'>Ver Reporte</button>"); sb.AppendLine(" </div>"); sb.AppendLine(" </div>"); /* btn-toolbar */ sb.AppendLine(" </div>"); /* container reportToolbar */ sb.AppendLine(" <div class='container-fluid reportContentContainer'>"); sb.AppendLine(" <div class='reportContent'>"); if (model.IsMissingAnyRequiredParameterValues(contentData.Parameters)) { sb.AppendLine(" <div class='reportInformation'>Por favor llene los parámetros y ejecute el reporte ...</div>"); } else { if (!model.AjaxLoadInitialReport) { 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>"); /* reportContent */ sb.AppendLine(" </div>"); /* container--fluid reportContentContainer */ sb.AppendLine(" </div>"); /* d-flex flex-column */ sb.AppendLine("</form>"); return(new HtmlString(sb.ToString())); }
public static string ParametersToHtmlString(System.Collections.Generic.List <ReportParameterInfo> parameters, ReportViewerModel model) { StringBuilder sb = new StringBuilder(); string selectedValue, tmpDateValue; string[] values; if (parameters == null) { var contentData = new ReportExportResult(); var definedParameters = ReportServiceHelpers.GetReportParameters(model, true); contentData.SetParameters(definedParameters, model.Parameters); parameters = contentData.Parameters; } if (parameters != null && parameters.Count > 0) { sb.AppendLine("<div class='d-flex parametersContainer'>"); sb.AppendLine(" <div class='d-flex flex-row row-cols-4 parameters'>"); foreach (var reportParameter in parameters) { sb.AppendLine(" <div class='form-group parameter'>"); if (reportParameter.PromptUser || model.ShowHiddenParameters) { sb.AppendLine($" <label class='pr-1' for='{reportParameter.Name}'>{reportParameter.Prompt.HtmlEncode()}</label>"); if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any()) { sb.AppendLine($" <select id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(reportParameter.MultiValue == true ? "multiple='multiple'" : "")}>"); foreach (var value in reportParameter.ValidValues) { sb.AppendLine($" <option value='{value.Value}' {(reportParameter.SelectedValues.Contains(value.Value) ? "selected='selected'" : "")}>{value.Label.HtmlEncode()}</option>"); } sb.AppendLine($" </select>"); } else { selectedValue = reportParameter.SelectedValues.FirstOrDefault(); if (reportParameter.Type == ReportExecutionService.ParameterTypeEnum.Boolean) { sb.AppendLine($" <input type='checkbox' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(selectedValue.ToBoolean() ? "checked='checked'" : "")} />"); } else if (reportParameter.Type == ReportExecutionService.ParameterTypeEnum.DateTime) { // Aplicar formato de fecha tmpDateValue = ConvertDateToString(selectedValue); sb.AppendLine($" <input type='datetime' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{tmpDateValue}' />"); } else { sb.AppendLine($" <input type='text' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{selectedValue}' />"); } } } else { if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any()) { values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray(); sb.AppendLine($" <input type='hidden' id='{reportParameter.Name}' name='{reportParameter.Name}' value='{String.Join(",", values)}' />"); } } sb.AppendLine(" </div>"); /* form-group */ } sb.AppendLine(" </div>"); sb.AppendLine($" <input type='hidden' id='ReportEnablePaging' name='ReportEnablePaging' value='{model.EnablePaging}' />"); sb.AppendLine("</div>"); } return(sb.ToString()); }