2

在我的 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 的新手。如果有人知道请分享

上述代码的参考在这里

4

2 回答 2

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"/>
   }
于 2013-02-14T01:44:59.757 回答
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>
于 2014-11-17T07:06:13.920 回答