使用 ASP .NET Core 呈现 .rdlc 报告

Rendering .rdlc reports with ASP .NET Core

是否可以使用 ASP.NET 核心呈现 .rdlc 报告?目前,这似乎只有在我针对 .NET Framework 而不是 .NET Core 时才有可能。

我不需要报表查看器,我只需要将 .rdlc 报表的结果呈现为字节数组。

您无法在 .NET Core 中使用 .rdlc 作为字节数组呈现 .rdlc 报告。

您可以很好地将 rdlc 呈现为字节数组。请参阅我前一段时间提出的相关问题。 .

最终对该线程的创造性讨论产生了一个 angular 包(https://www.npmjs.com/package/ng2-pdfjs-viewer - 披露;我是作者)在客户端具有可使用的 rdlc 字节数组功能。当然,您可以选择另一个 javascript 库来显示字节数组,而不是这个包。

angular 上的简单用法是这样的。请注意,即使您使用纯 js 或其他框架,大部分代码也可以重复使用。

下面的代码演示

1. 在 aspnet 核心操作方法(在服务器端)上使用 RDLC 报告查看器控件吐出字节数组,并使用 http 通过网络发送它。 (代码在 C# 中)
2. 将响应的字节数组处理成blob对象(Js)
3。将 blob 对象送入 ng2-pdfjs-viewer.
4。 ng2-pdfjs-viewer 内部使用 Mozilla 的 PDFJS 来完成在浏览器上显示 PDF 的壮举。
(仅供参考。我从 ng2-pdfjs-viewer 包中提供的示例中获取了代码。如果您使用的是其他库或纯 javascript,请替换第 3 步和第 4 步)

<!-- your.component.html -->
<button (click)="showPdf();">Show</button>
<div style="width: 800px; height: 400px">
  <ng2-pdfjs-viewer #pdfViewer></ng2-pdfjs-viewer>
</div>

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 showPdf() {
    let url = "http://localhost/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")
}

呈现 RDLC 报告依赖于 WinForms 和 WebForms,目前仅在 .NET Framework 上受支持。希望 Microsoft 将在 .NET Core 或 .NET 5 上提供一个(精简版)版本(只是为初学者呈现报告),但目前还没有消息。

作为替代方案,您可以寻求一种解决方案,您 运行 将渲染报告为单独的 ASP.NET 2 应用程序,目标为 .NET Framework,而应用程序的其余部分可以目标为 .NET Core 3 或更高版本。这样,您就可以使用适当的数据调用报告端点,并且它 returns 呈现报告。

这就是我所做的,创建了一种微服务架构,其中多个 .Net Core 3.1 应用程序可以 post XML RDLC 报告定义和数据到端点 运行ning on net48,它使用 LocalReport class 将报告呈现为所需的格式,并将其作为字节数组返回。

Angular app |  ---->  |  APIs (.Net Core 3.1)  |  ---->  |  API (.Net Framework 4.8)

如果您想使用 rdlc 报告创建 pdf/excel/word,我建议您可以使用 AspNetCore.Reporting 库。这是开源的,以 nuget 包的形式提供。您可以将其集成到您的 .NET Core API 或 .NET Core Azure 函数中。您可以生成一个字节数组,将其转换为 base 64 字符串并将其检索到您的客户端。更多关于评论中的link。

如果有人仍在寻找类似的解决方案,我建议使用“ReportViewerCore.NETCore”。

这是 nuGet 参考 - https://www.nuget.org/packages/ReportViewerCore.NETCore/

这是回购的 github link - https://github.com/lkosson/reportviewercore/

基本用法

Stream reportDefinition; // your RDLC from file or resource
IEnumerable dataSource; // your datasource for the report

LocalReport report = new LocalReport();
report.LoadReportDefinition(reportDefinition);
report.DataSources.Add(new ReportDataSource("source", dataSource));
report.SetParameters(new[] { new ReportParameter("Parameter1", "Parameter value") });
byte[] pdf = report.Render("PDF");