Asp.net-Core

使用 ASP .NET Core 呈現 .rdlc 報告

  • April 8, 2022

.rdlc是否可以使用 ASP.NET Core呈現報告?目前,這似乎只有在我針對 .NET Framework 而不是 .NET Core 時才有可能。

我不需要報表查看器,我只需要將.rdlc報表的結果呈現為字節數組。

如果您想使用 rdlc 報告創建 pdf/excel/word,我建議您可以使用 AspNetCore.Reporting 庫。這是開源的,作為一個 nuget 包提供。您可以將其集成到您的 .NET Core API 或 .NET Core Azure 函式中。您可以生成一個字節數組,將其轉換為 base 64 字元串並將其檢索到您的客戶端。更多關於評論中的連結。

您可以很好地將 rdlc 呈現為字節數組。請參閱我不久前提出的一個相關問題。RDLC ASP.NET Core 和 Angular(>2.0) 的本地報表查看器

最終,對該執行緒的創造性討論導致了一個角度包(https://www.npmjs.com/package/ng2-pdfjs-viewer - 披露;我是作者)在客戶端具有可消耗的 rdlc 字節數組功能。當然,您可以選擇另一個 javascript 庫來顯示字節數組,而不是這個包。

角度的簡單用法是這樣的。請注意,即使您使用的是純 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")
}

引用自:https://stackoverflow.com/questions/54932390