2012-07-20 73 views
2

在我MVC3剃刀应用程序,我用下面的代码报告报告观众3剃刀

位指示

ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer(); 
    rv.ProcessingMode = ProcessingMode.Local; 
    rv.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc"); 
    rv.LocalReport.Refresh(); 

    byte[] streamBytes = null; 
    string mimeType = ""; 
    string encoding = ""; 
    string filenameExtension = ""; 
    string[] streamids = null; 
    Warning[] warnings = null; 

    streamBytes = rv.LocalReport.Render("RDLC", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); 

    return File(streamBytes, mimeType, "TestReport.rdlc"); 

ASPX视图

<div> 
     <script runat="server"> 
      private void Page_Load(object sender, System.EventArgs e) 
      { 
       ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc"); 
       ReportViewer1.LocalReport.Refresh(); 
      } 
     </script> 
     <form id="Form1" runat="server" method="get" action="/Pag1/File"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server">   
     </asp:ScriptManager> 
     <rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer> 
     </form>   
    </div> 

这里我得到了PDF格式需要用pdfviewer打开的结果。我只想display the report in viewr。我是MVC3的新手。 如果有任何身体知道请分享

参考上面的代码是here

+0

您正在使用MVC3剃刀?那么aspx是什么?你不是使用cshtml吗? – friend 2013-01-29 03:09:21

回答

1

下面是一个ActionMethod我在我的网站的一个用于生成报告的例子:

public ActionResult WeeklyAisleReport(DateTime start, DateTime end) 
    { 
     var range = new DateRange(start, end); 
     var records = _repository.Select(range, ""); 
     var formattedRecords = AisleProductivityRecord.Generate(records).ToList(); 

     var localReport = new LocalReport 
     { 
      ReportPath = 
       Server.MapPath("~/Content/Reports/PTLWeeklyProductivity.rdlc") 
     }; 


     var pickRecords = new ReportDataSource("PickRecords",formattedRecords); 

     localReport.DataSources.Add(pickRecords); 


     const string ReportType = "PDF"; 
     string mimeType; 
     string encoding; 
     string fileNameExtension; 


     Warning[] warnings; 
     string[] streams; 

     //Render the report 
     byte[] renderedBytes = localReport.Render(
      ReportType, 
      null, //deviceInfo, 
      out mimeType, 
      out encoding, 
      out fileNameExtension, 
      out streams, 
      out warnings); 
     Response.AddHeader("content-disposition", 
          "attachment; filename=WeeklyAisleReport-" + start.ToString("yyyy_MM_dd") + "." + 
          fileNameExtension); 
     return File(renderedBytes, mimeType); 
    } 

关于查看,您不能在MVC应用程序中使用WebForms标签(等等)。您需要创建一个表单发布到生成PDF的ActionMethod。

你的Razor视图文件应该是这个样子(使用我的方法为例):

@using (Html.BeginForm("WeeklyAisleReport", "PTL")) 
    { 
     @Html.TextBox("start") 
     @Html.TextBox("end") 
     <input type="submit" value="View Report"/> 
    } 
1

从工具箱你要跟ASPX页面添加报表查看器和脚本管理。

SizeToReportContent = "true" 

不要忘了上述属性添加到您的报表查看器

然后你的页面看起来像下面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportView.aspx.cs" Inherits="ERP.Reports.ReportView" %> 

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="true" ShowPrintButton="true" Height="649px" Width="1105px" SizeToReportContent = "true"> 

    </rsweb:ReportViewer> 

    </div> 
     <script> 

     </script> 
    </form> 

</body> 
</html> 

在您的ASPX加入今典代码页

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      RenderReportModels(); 
     } 
    } 
    private void RenderReportModels() 
    { 

     List<Company> comp = new List<Company>(); 
     CompanyBAL bal = new CompanyBAL(); 
     comp = bal.SampleData();//Load Data 

     // Clear out any previous data sources. 
     this.ReportViewer1.LocalReport.DataSources.Clear(); 

     // Set report mode for local processing. 
     ReportViewer1.ProcessingMode = ProcessingMode.Local; 

     // Validate report source. 
     var rptPath = Server.MapPath(@"./rdlc/MyReport.rdlc"); 

     //@"E:\ERP\Reports\rdlc\MyReport.rdlc"; 

     if (!File.Exists(rptPath)) 
      return; 

     // Set report path. 
     this.ReportViewer1.LocalReport.ReportPath = rptPath; 



     // Load the dataSource. 
     var ds1 = ReportViewer1.LocalReport.GetDataSourceNames(); 
     ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds1[0], comp)); 


     // Refresh the ReportViewer. 
     ReportViewer1.LocalReport.Refresh(); 
    } 

添加一个控制器和相应的视图(CSHTML)下面的代码添加到视图页面

<div> 
<input type="submit" onclick="return ReportValidationCheck();" name="ShowReport" 
     value="Show Report" /> 
</div> 
<iframe id="ifrmReportViewer" frameborder="0" width="1000" height="800" style="overflow:hidden;" scrolling="no"></iframe> 

<script> 
    function ReportValidationCheck() { 
     var url = "../Reports/ReportView.aspx"; //your ASPX page path 
     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; 
    } 
</script>