RDLC Local report viewer for ASP.NET Core and Angular(>2.0)

落爺英雄遲暮 提交于 2019-11-28 23:50:52

Found an npm package ng2-pdfjs-viewer, though it is not quite the MS report viewer, if you are willing to use PDFJS, the documentation of the package has an example on similar lines to use LocalReport viewer for generating pdf and ng2-pdfjs-viewer to display it in browser - (https://www.npmjs.com/package/ng2-pdfjs-viewer)

<!-- your.component.html -->
<button (click)="openPdf();">Open Pdf</button>
<!-- your.component.ts-->
export class MyComponent implements OnInit {
  @ViewChild('pdfViewer') pdfViewer
  ...

  private downloadFile(url: string): any {
    return this.http.get(url, { responseType: ResponseContentType.Blob }).map(
      (res) => {
        return new Blob([res.blob()], { type: "application/pdf" });
      });
  }

  public openPdf() {
    let url = "http://localhost:4200/api/GetMyPdf
    this.downloadFile(url).subscribe(
    (res) => {
        this.pdfViewer.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
        this.pdfViewer.refresh(); // Ask pdf viewer to load/reresh pdf
      }
    );
  }
[HttpGet]
[Route("MyReport")]
public IActionResult GetReport()
{
   var reportViewer = new ReportViewer {ProcessingMode = ProcessingMode.Local};
   reportViewer.LocalReport.ReportPath = "Reports/MyReport.rdlc";

   reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource1", reportObjectList1));
   reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource2", reportObjectList1));

   Warning[] warnings;
   string[] streamids;
   string mimeType;
   string encoding;
   string extension;

   var bytes = reportViewer.LocalReport.Render("application/pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);

   return File(bytes, "application/pdf")
}

Microsoft is not implementing or bringing RDLC report viewer into aspnet core. Instead they are purchasing a product to fill the void.

Full link to news - https://blogs.msdn.microsoft.com/sqlrsteamblog/2018/04/02/microsoft-acquires-report-rendering-technology-from-forerunner-software/

Link to original issue - https://github.com/aspnet/Home/issues/1528

Here is the essence. "Microsoft acquires report rendering technology from Forerunner Software

We’re pleased to announce that we’ve acquired technology from Forerunner Software to accelerate our investments in Reporting Services. This technology includes, among other things, client-side rendering of Reporting Services (*.rdl) reports, responsive UI widgets for viewing reports, and a JavaScript SDK for integrating reports into other apps – a testament to what our partners can achieve building on our open platform.

This is great news for you, as we see opportunities to apply this technology to multiple points of feedback we’ve heard from you:

You’re looking for cloud Software-as-a-Service (SaaS) or Platform-as-a-Service (PaaS) that can run SSRS reports. As you might’ve seen in our Spring ’18 Release Notes, we’re actively working on bringing SSRS reports to the Power BI cloud service, and we’re building on client-side rendering to make that possible. You want to view SSRS reports on your phone, perhaps using the Power BI app. We believe this technology will help us deliver better, more responsive UI for supplying report parameter values, navigating within reports, and possibly even viewing report content.

You love the Report Viewer control… but it’s an ASP.NET Web Forms control. You need something you can integrate into your ASP.NET Core/MVC app or non-ASP.NET app. With this technology, we hope to deliver a client-side/JavaScript-based Report Viewer you can integrate into any modern app.

These are large undertakings and we don’t yet have timeframes to share, but stay tuned over the coming months as we always strive to share our progress with you and hear your feedback as early and often as we can.

Forerunner Software will continue to support existing customers for a limited period of time."

lienn

If the question is how to use Microsoft Reportviewer on ASP.NET Core project, regardless of implementation details, my solution is to bypass the actual reportviewer control and render reports directly to PDF or Excel. It works in .net Core 1.1. NuGet package we use is Microsoft.ReportViewer.2012.Runtime by Fornax.

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Reporting.WebForms;

namespace WebApplication3.Controllers
{
    public class ReportController : Controller
    {
        private readonly IHostingEnvironment environment = null;
        public ReportController(IHostingEnvironment environment)
        {
            this.environment = environment;
        }
        public IActionResult Report()
        {
            string mimeType;
            string encoding;
            string filenameExtension;
            string[] streams;
            Warning[] warnings;
            var rv = new ReportViewer();
            rv.ProcessingMode = ProcessingMode.Local;
            rv.LocalReport.ReportPath = Path.Combine(environment.ContentRootPath, "Reports", "Report1.rdlc");
            rv.LocalReport.Refresh();
            var bytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streams, out warnings);
            return File(bytes, mimeType);
        }
    }
}

In order for Jame's solution to work - it requires that you reference the full .NET Framework. This is all well and good for ASP.NET Core 1 and 2, however - as everyone should be aware by now - ASP .NET 3 will NOT allow you to reference the full .NET Framework.

Currently, it's only possible to use SSRS hosted server reports (RDL) reports with .NET Core. For client RDLC reports, currently only the paid Syncfusion solution works (I've tested the trail version)

James solution will is entirely invalid with ASP.NET Core 3 (which again - only allows you to reference .NET Core - not the .NET Framework)

waqar iftikhar
public List<object> _dataSourceList = new List<object>();
public string _dataSourceName { get; set; }

public string _reportPath = CommonUtil.Report_path; //set your report path in app.config file
public Dictionary<string, string> Parameters = new Dictionary<string, string>();

public void PDFPrint_Load() {

  string mimtype="";
  int extension = 1;

  LocalReport localReport= new LocalReport(_reportPath);
  localReport.AddDataSource(_dataSourceName, _dataSourceList);

  if (Parameters != null && Parameters.Count > 0)// if you use parameter in report
  {
    List<ReportParameter> reportparameter = new List<ReportParameter>();
    foreach (var record in Parameters) {
      reportparameter.Add(new ReportParameter());
    }
  }
  var result = localReport.Execute(RenderType.Pdf, extension,parameters: 
  Parameters, mimtype);

  byte[] bytes = result.MainStream;
  string fileName = "Report.pdf";
  return File(bytes , "application/pdf",fileName );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!