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

这是我的第一个问题。

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

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

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

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

我是RDLC的新手,我以前从未使用过数据集。

谢谢

您可以使用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 { 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"); } 

在ASP.NET MVC中以HTML格式呈现RDLC报表

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

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

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

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

           
  6. 现在,您需要创建一个DataSet,以将其作为DataSource提供给报表。 所以添加一个新的DataSet(.xsd文件)。 您可以在该数据集中使用表或存储过程。 只需打开服务器资源管理器,然后拖放数据源(表或存储过程)。

  7. 设计报告:
    添加一个新的.rdlc文件,然后转到视图>报告数据面板并添加一个新的数据源(选择该.xsd文件),然后选择该.xsd文件中的哪个源将用作该报告的DataSet。
  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返回的服务函数。

希望有所帮助。