我成功地制作了一个原型,借助于this article,在PDF,Excel和TIFF图像中呈现RDLC报告.但是,我很惊讶,HTML不是LocalReport.Render()中的默认可用格式之一.
我遇到了this article,它描述了一个技巧来启用HTML4.0的渲染格式,但我认为这只是一个ReportViewer控件(我可能是错的).
问题是,在MVC中如何在HTML中渲染RDLC报告,就像ReportView一样(见下面的屏幕截图)?
解决方法
>在解决方案中创建一个文件夹,并给出一个名称“报告”.
>添加ASP.Net Web窗体并将其命名为ReportView.aspx
>创建一个类ReportData并将其添加到Reports文件夹.添加以下代码
到班级.
public class ReportData { public ReportData() { this.ReportParameters = new List<Parameter>(); this.DataParameters = new List<Parameter>(); } public bool IsLocal { get; set; } public string ReportName { get; set; } public List<Parameter> ReportParameters { get; set; } public List<Parameter> DataParameters { get; set; } } public class Parameter { public string ParameterName { get; set; } public string Value { get; set; } }
>添加另一个类并将其命名为ReportBasePage.cs.在此类中添加以下代码.
public class ReportBasePage : System.Web.UI.Page { protected ReportData ReportDataObj { get; set; } protected override void OnInit(EventArgs e) { base.OnInit(e); if (HttpContext.Current != null) if (HttpContext.Current.Session["ReportData"] != null) { ReportDataObj = HttpContext.Current.Session["ReportData"] as ReportData; return; } ReportDataObj = new ReportData(); CaptureRouteData(Page.Request); } private void CaptureRouteData(HttpRequest request) { var mode = (request.QueryString["rptmode"] + "").Trim(); ReportDataObj.IsLocal = mode == "local" ? true : false; ReportDataObj.ReportName = request.QueryString["reportname"] + ""; string dquerystr = request.QueryString["parameters"] + ""; if (!String.IsNullOrEmpty(dquerystr.Trim())) { var param1 = dquerystr.Split(','); foreach (string pm in param1) { var rp = new Parameter(); var kd = pm.Split('='); if (kd[0].Substring(0,2) == "rp") { rp.ParameterName = kd[0].Replace("rp",""); if (kd.Length > 1) rp.Value = kd[1]; ReportDataObj.ReportParameters.Add(rp); } else if (kd[0].Substring(0,2) == "dp") { rp.ParameterName = kd[0].Replace("dp",""); if (kd.Length > 1) rp.Value = kd[1]; ReportDataObj.DataParameters.Add(rp); } } } } }
>将ScriptManager添加到ReportView.aspx页面.现在把报表查看器带到页面.在报表查看器中,设置属性AsyncRendering =“false”.代码如下.
<rsweb:ReportViewer ID="ReportViewerRSFReports" runat="server" AsyncRendering="false" Width="1271px" Height="1000px" > </rsweb:ReportViewer>
>在ReportView.aspx.cs中添加两个NameSpace
using Microsoft.Reporting.WebForms; using System.IO;
>将System.Web.UI.Page更改为ReportBasePage.只需使用以下代码替换代码.
public partial class ReportView : ReportBasePage { protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { RenderReportModels(this.ReportDataObj); } } private void RenderReportModels(ReportData reportData) { RASolarERPData dal = new RASolarERPData(); List<ClosingInventoryValuation> objClosingInventory = new List<ClosingInventoryValuation>(); // Reset report properties. ReportViewerRSFReports.Height = Unit.Parse("100%"); ReportViewerRSFReports.Width = Unit.Parse("100%"); ReportViewerRSFReports.CssClass = "table"; // Clear out any prevIoUs datasources. this.ReportViewerRSFReports.LocalReport.DataSources.Clear(); // Set report mode for local processing. ReportViewerRSFReports.ProcessingMode = ProcessingMode.Local; // Validate report source. var rptPath = Server.MapPath(@"./Report/" + reportData.ReportName +".rdlc"); //@"E:\RSFERP_SourceCode\RASolarERP\RASolarERP\Reports\Report\" + reportData.ReportName + ".rdlc"; //Server.MapPath(@"./Report/ClosingInventory.rdlc"); if (!File.Exists(rptPath)) return; // Set report path. this.ReportViewerRSFReports.LocalReport.ReportPath = rptPath; // Set report parameters. var rpPms = ReportViewerRSFReports.LocalReport.GetParameters(); foreach (var rpm in rpPms) { var p = reportData.ReportParameters.SingleOrDefault(o => o.ParameterName.ToLower() == rpm.Name.ToLower()); if (p != null) { ReportParameter rp = new ReportParameter(rpm.Name,p.Value); ReportViewerRSFReports.LocalReport.SetParameters(rp); } } //Set data paramater for report SP execution objClosingInventory = dal.ClosingInventoryReport(this.ReportDataObj.DataParameters[0].Value); // Load the dataSource. var dsmems = ReportViewerRSFReports.LocalReport.GetDataSourceNames(); ReportViewerRSFReports.LocalReport.DataSources.Add(new ReportDataSource(dsmems[0],objClosingInventory)); // Refresh the ReportViewer. ReportViewerRSFReports.LocalReport.Refresh(); } }
>将文件夹添加到报告文件夹,并将其命名为“报告”.现在,将RDLC报告添加到Reports / Report文件夹,并将其命名为ClosingInventory.rdlc.
>现在添加一个Controller并将其命名为ReportController.在控制器中添加以下操作方法.
public ActionResult ReportViewer() { ViewData["reportUrl"] = "../Reports/View/local/ClosingInventory/"; return View(); }
>添加一个视图页面,点击ReportViewer控制器.命名为查看页面ReportViewer.cshtml.将以下代码添加到视图页面.
@using (Html.BeginForm("Login")) { @Html.DropDownList("ddlYearMonthFormat",new SelectList(ViewBag.YearMonthFormat,"YearMonthValue","YearMonthName"),new { @class = "DropDown" }) Stock In Transit: @Html.TextBox("txtStockInTransit","",new { @class = "LogInTextBox" }) <input type="submit" onclick="return ReportValidationCheck();" name="ShowReport" value="Show Report" /> }
frameborder="0" width="1000"; height="1000"; style="overflow:hidden;" scrolling="no"
>将以下JavaScript添加到查看器.
function ReportValidationCheck() { var url = $('#hdUrl').val(); var yearmonth = $('#ddlYearMonthFormat').val(); var stockInTransit = $('#txtStockInTransit').val() if (stockInTransit == "") { stockInTransit = 0; } if (yearmonth == "0") { alert("Please Select Month Correctly."); } else { //url = url + "dpSpYearMonth=" + yearmonth + ",rpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit; url = "../Reports/ReportView.aspx?rptmode=local&reportname=ClosingInventory¶meters=dpSpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit; var myframe = document.getElementById("ifrmReportViewer"); if (myframe !== null) { if (myframe.src) { myframe.src = url; } else if (myframe.contentWindow !== null && myframe.contentWindow.location !== null) { myframe.contentWindow.location = url; } else { myframe.setAttribute('src',url); } } } return false; }
>在Web.config文件中添加以下密钥到appSettings部分添加
key="UnobtrusiveJavaScriptEnabled" value="true"
>在system.web处理程序中添加以下键
add =“*”path =“Reserved.ReportViewerWebControl.axd”type =“Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms,Version = 10.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”>将您的数据源更改为您自己的数据源.这个解决方案很简单,我觉得每个人都喜欢它.