FastReport.Net 2018新功能之一:使用nuget數據包,要安裝軟件包,必須創建一個本地軟件包源並將已編譯的FastReport庫放在其中(對於授權的軟件包,此機制已保留)。
創建.Net Core應用程序,調用解決方案的上下文菜單,然後選擇Manage NuGet Packages項。
選擇Packages source——nuget.org,在搜索欄中鍵入FastReport。
用戶需要安裝兩個軟件包:FastReport.Core和FastReport.Web。
第一個是.Net Core框架的實際FastReport庫,第二個是WebReport對象,允許用戶在瀏覽器中顯示報表並對其進行管理。
要在應用程序中顯示報表,需要一個報表文件。從FastReport.Net中獲取Master-Detail.frx報告模板和nwind.xml數據庫文件,將它們放在Reports文件夾中,先在wwwroot的根目錄中創建它:
打開HomeController類
需要一個接口IHostEnviroment,使用它獲取有關環境的信息,具體來說,需要WebRootPath屬性來指定報表文件和數據庫的路徑。因此,添加一個類的構造函數,該類將接口作爲參數。
private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; }
在Index方法中,編寫以下代碼:
public IActionResult Index() { string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to the wwwroot folder WebReport webReport = new WebReport(); // Create a Web Report Object webReport.Report.Load(webRootPath + "/reports/Master-Detail.frx"); // Load the report into the WebReport object var dataSet = new DataSet(); // Create a data source dataSet.ReadXml(webRootPath + "/reports/nwind.xml"); // Open the xml database webReport.Report.RegisterData(dataSet, "NorthWind"); // Register the data source in the report ViewBag.WebReport = webReport; return View(); }
要在網頁上顯示WebReport對象,請更改Index.cshtml視圖,如下所示:
@{ ViewData["Title"] = "Home Page"; } @await ViewBag.WebReport.Render();
在Startup.cs文件中,需要進行更改,代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseFastReport(); … }
現在運行應用程序:
使用FastReport.Core會更加容易,但是,仍然必須從本地源安裝許可包,不過配置NuGet包的本地源只需要一分鐘的時間。