[ASP.NET] Visual Studio 2022使用rdlc報表

ReportViewer產PDF報表其實真的很好用~但新的版本好像沒辦法裝完SSDT就可以使用…

 

【開發工具】

Visual Studio 2022

【使用套件】

ReportViewerForMvc14 >>Nuget連結<<

【使用模組】

Microsoft RDLC Report Designer 2022 >>Marketplace連結<<

【步驟】
1. 開啟專案後發現沒有可以新增報表(.rdlc)的選項。

2. 安裝Microsoft RDLC Report Designer 2022。

3. 安裝完成後可以在延伸模組管理員內看到Rdlc報表設計工具(如下圖)。

4. 此時新增項目就可以找到「報表」,這個就是我們熟悉的rdlc了。

5. 安裝Report Viewer套件。

6. 資料設定可以參考先前的文章『[ASP.NET] MVC使用rdlc報表』。

【補充】
由於我們產表常常會是後端產完就好,另外再進行運用,因此記錄一下產檔程式碼如下。

Microsoft.Reporting.WebForms.ReportViewer reportViewer = new Microsoft.Reporting.WebForms.ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportPath = $"RDLC檔案的實體路徑";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("RDLC內資料來源的名稱", data));

string mimeType, encoding, fileNameExtension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes = reportViewer.LocalReport.Render(
    "PDF", // 指定輸出格式
    null,  // 預設裝置資訊
    out mimeType, out encoding, out fileNameExtension,
    out streams, out warnings
);
File.WriteAllBytes("檔案要儲存的實際位置",pdfBytes);