MVC 5 How to use an object in rdlc report

后端 未结 2 1496
后悔当初
后悔当初 2021-01-02 11:48

this is my first question here.

I\'m using VS Community 2015 and a MVC 5 project with Entity Framework 6. I use code first migration for data modeling.

I all

相关标签:
2条回答
  • 2021-01-02 11:58

    You can use the ReportViewer object to render an RDLC to PDF or HTML. For my case (below) I wanted a PDF document and I returned it as a FileContentResult ActionResult. If you want it to return as a download use the File ActionResult (I've commented that out for your use).

    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

    0 讨论(0)
  • 2021-01-02 12:03

    You need to use ReportViewer. Here is a walkthrough for generating report from any Table or Stored Procedure. But from Visual Studio 2017 there is no ReportViewer tool by default. So to generate a report, first you need to configure several things.

    1. Report Designer:
      Go to Tools > Extensions and Updates. Then download and install Microsoft Rdlc Report Designer for Visual Studio.

    2. ReportViewer Controll:
      Open package manager console and run this: install-package Microsoft.ReportingServices.ReportViewerControl.WebForms.

    3. Add the ReportViewer Controll to ToolBox:
      From ToolBox right click on General and select "Choose Items". After the loading is finished, click to Browse, then navigate to the project folder (the report viewer dll is located in the packages folder of the project). Go to Microsoft.ReportingServices.ReportViewerControl.WebForms\lib\net40 folder and add WebForms.dll.
    4. From pm console run: install-package ReportViewerForMvc
    5. A default "ReportViewerWebForm.aspx" will be added to the root location. Make sure it contains a ScriptManager and a ReportViewer. If not then simply add them from ToolBox or copy paste this:

      <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. Now you need to create a DataSet to provide it to the report as its DataSource. So add a new DataSet (.xsd file). You can use both Tables or Stored Procedures in that Data Set. Simply open the Server Explorer and then drag and drop the data source (Table or Stored Procedure).

    7. Design a Report:
      Add a new .rdlc file then go to the View > Report Data panel and add a new Data Source (choose that .xsd file), then choose which source from that .xsd file will be used as the DataSet for that report.
    8. Generate Report:
      Now call the default "ReportViewerWebForm.aspx" like this:
      From caller Controller:

      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();  
      

      From caller View (cshtml file):

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

      In this line

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

      the ReportDataSet is the data set name used when configuring that .rdlc file, and the _createEntryService.GetMoneyReceiptReport(model.Patient.PatientId)) is a service function that calls a stored procedure and return it as a DataTable.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题