这是我的第一个问题。
我正在使用VS Community 2015和一个带有Entity Framework 6的MVC 5项目。我使用代码优先迁移进行数据建模。
我已经有报告使用每个视图。每个视图都使用C#模型将show report作为HTML5网页。
现在我必须将输出发送到pdf,word,excel ......为此,我将使用RDLC,但我不知道如何将对象模型设置为数据集。这个想法是发送相同的对象,它已经使用视图来构建报告。报告的数据不是。
任何想法,建议或教程我该怎么做?
我对RDLC很新,我以前从未使用过数据集。
感谢
答案 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");
}
答案 1 :(得分:4)
您需要使用ReportViewer。这是从任何表或存储过程生成报告的演练。但是从Visual Studio 2017开始,默认情况下没有ReportViewer工具。因此,要生成报告,首先需要配置几件事。
报表设计器:
转到工具>扩展和更新。然后下载并安装 Visual Studio的Microsoft Rdlc报表设计器。
ReportViewer控件:
打开程序包管理器控制台并运行以下程序:安装程序包Microsoft.ReportingServices.ReportViewerControl.WebForms 。
默认的“ 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>
现在,您需要创建一个数据集以将其作为数据源提供给报表。因此,添加一个新的DataSet(.xsd文件)。您可以在该数据集中使用表或存储过程。只需打开服务器资源管理器,然后拖放数据源(表或存储过程)。
生成报告:
现在像这样调用默认的“ 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返回。
希望有帮助。