【问题标题】:Report viewer in asp.net mvc 3 razorasp.net mvc 3 razor 中的报告查看器
【发布时间】:2012-07-20 11:23:50
【问题描述】:

在我的 MVC3 razor 应用程序中,我使用以下代码进行报告

控制器

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

【问题讨论】:

  • 您使用的是 MVC3 Razor?那这个aspx是干什么用的?你不是用cshtml吗?

标签: asp.net-mvc-3 razor rdlc report-viewer2010


【解决方案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"/>
   }

【讨论】:

    【解决方案2】:

    从工具箱向您的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2017-04-11
      相关资源
      最近更新 更多