【问题标题】:MVC 5 How to use an object in rdlc reportMVC 5 如何在 rdlc 报告中使用对象
【发布时间】:2018-07-07 17:05:26
【问题描述】:

这是我的第一个问题。

我正在使用 VS Community 2015 和一个带有 Entity Framework 6 的 MVC 5 项目。我使用代码优先迁移进行数据建模。

我已经为每个报告使用了一个视图。每个视图都使用 C# 模型将报告显示为 HTML5 网页。

现在我必须将输出发送到 pdf、word、excel...为此我将使用 RDLC,但我不知道如何将对象模型设置为数据集。这个想法是发送已经使用视图来构建报告的相同对象。报告的数据不是。

任何想法或建议或教程我该怎么做?

我对 RDLC 很陌生,以前从不使用数据集。

谢谢

【问题讨论】:

    标签: c# asp.net-mvc-5 rdlc


    【解决方案1】:

    您可以使用 ReportViewer 对象将 RDLC 呈现为 PDF 或 HTML。对于我的情况(如下),我想要一个 PDF 文档并将其作为 FileContentResult ActionResult 返回。如果您希望它作为下载返回,请使用 File ActionResult(我已将其注释掉以供您使用)。

    public ActionResult GetPackingSlipPDF(int shipmentId)
        {
            var shipment = _inboundShipmentService.GetInboundShipmentById(shipmentId);
    
            Warning[] warnings;
            string mimeType;
            string[] streamids;
            string encoding;
            string filenameExtension;
    
            var viewer = new ReportViewer();
            viewer.LocalReport.ReportPath = @"Labels\PackingSlip.rdlc";
    
            var shipLabel = new ShippingLabel { ShipmentId = shipment.FBAShipmentId, Barcode = GetBarcode(shipment.FBAShipmentId) };
    
            viewer.LocalReport.DataSources.Add(new ReportDataSource("ShippingLabel", new List<ShippingLabel> { shipLabel }));
            viewer.LocalReport.Refresh();
    
            var bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
    
            return new FileContentResult(bytes, mimeType);
    
            //return File(bytes, mimeType, shipment.FBAShipmentId + "_PackingSlip.pdf");
        }
    

    Rendering an RDLC report in HTML in ASP.NET MVC

    【讨论】:

    • 感谢 Muhammad!,我在这里学习了很多新东西。
    • 如何设计 rdlc 报告?它要求数据集或数据源...它迫使我使用与 SQL 的数据连接,但我想要一个 C# 模型对象作为数据源。
    【解决方案2】:

    您需要使用 ReportViewer。这是从任何表或存储过程生成报告的演练。但从 Visual Studio 2017 开始,默认情况下没有 ReportViewer 工具。所以要生成报告,首先你需要配置几件事。

    1. 报表设计器:
      转到工具 > 扩展和更新。然后下载并安装 Microsoft Rdlc Report Designer for Visual Studio

    2. ReportViewer 控件:
      打开包管理器控制台并运行:install-package Microsoft.ReportingServices.ReportViewerControl.WebForms

    3. 将 ReportViewer 控件添加到工具箱:
      从工具箱中右键单击常规并选择“选择项目”。加载完成后,点击 Browse,然后导航到项目文件夹(report viewer dll 位于项目的 packages 文件夹中)。转到 Microsoft.ReportingServices.ReportViewerControl.WebForms\lib\net40 文件夹并添加 WebForms.dll
    4. 从 pm 控制台运行:install-package ReportViewerForMvc
    5. 默认的“ReportViewerWebForm.aspx”将被添加到根位置。确保它包含 ScriptManagerReportViewer。如果没有,那么只需从 ToolBox 添加它们或复制粘贴:

      <asp:ScriptManager ID="ScriptManager1" runat="server">
              <Scripts>
                  <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
              </Scripts>
      </asp:ScriptManager>
      <rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>  
      
    6. 现在您需要创建一个 DataSet 以将其作为其 DataSource 提供给报表。所以添加一个新的数据集(.xsd 文件)。您可以在该数据集中同时使用表或存储过程。只需打开服务器资源管理器,然后拖放数据源(表或存储过程)。

    7. 设计报告:
      添加一个新的 .rdlc 文件,然后转到 View > Report Data 面板并添加一个新的数据源(选择该 .xsd 文件),然后从该 .xsd 文件中选择哪个源将用作该报告的数据集。
    8. 生成报告:
      现在像这样调用默认的“ReportViewerWebForm.aspx”:
      来自调用者控制器:

      var reportViewer = new ReportViewer();
      reportViewer.LocalReport.ReportPath =                         
      Server.MapPath("~/Reports/Reception/PatientMoneyReceipt.rdlc");
      reportViewer.LocalReport.DataSources.Clear();
      reportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportDataSet",
          _createEntryService.GetMoneyReceiptReport(model.Patient.PatientId)));
      reportViewer.LocalReport.Refresh();
      reportViewer.ProcessingMode = ProcessingMode.Local;
      reportViewer.AsyncRendering = false;
      reportViewer.SizeToReportContent = true;
      reportViewer.ZoomMode = ZoomMode.FullPage;
      
      ViewBag.ReportViewer = reportViewer;
      return View();  
      

      来自调用者视图(cshtml文件):

      @using ReportViewerForMvc
      ...
      @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)  
      

      在这一行

      reportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportDataSet",    _createEntryService.GetMoneyReceiptReport(model.Patient.PatientId)));  
      

      ReportDataSet 是配置该 .rdlc 文件时使用的数据集名称,_createEntryService.GetMoneyReceiptReport(model.Patient.PatientId)) 是调用存储过程并将其作为 DataTable 返回的服务函数。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多