MVC 5如何在rdlc报告中使用对象

时间:2016-08-19 15:33:05

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

这是我的第一个问题。

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

我已经有报告使用每个视图。每个视图都使用C#模型将show report作为HTML5网页。

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

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

我对RDLC很新,我以前从未使用过数据集。

感谢

2 个答案:

答案 0 :(得分:6)

您可以使用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

答案 1 :(得分:4)

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

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

  2. ReportViewer控件:
    打开程序包管理器控制台并运行以下程序:安装程序包Microsoft.ReportingServices.ReportViewerControl.WebForms

  3. 将ReportViewer控件添加到工具箱:
    在工具箱中,右键单击常规,然后选择“选择项目”。加载完成后,单击“浏览”,然后导航到项目文件夹(报表查看器dll位于项目的packages文件夹中)。转到 Microsoft.ReportingServices.ReportViewerControl.WebForms \ lib \ net40 文件夹,然后添加 WebForms.dll
  4. 从pm控制台运行:安装软件包ReportViewerForMvc
  5. 默认的“ ReportViewerWebForm.aspx”将添加到根位置。确保它包含 ScriptManager ReportViewer 。如果不是,那么只需从工具箱中添加它们或复制粘贴此内容:

    <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(.xsd文件)。您可以在该数据集中使用表或存储过程。只需打开服务器资源管理器,然后拖放数据源(表或存储过程)。

  7. 设计报告:
    添加一个新的.rdlc文件,然后转到“视图”>“报表数据”面板,并添加一个新的数据源(选择该.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返回。

希望有帮助。