原創(chuàng)|使用教程|編輯:王香|2018-07-09 10:07:08.000|閱讀 698 次
概述:如何在ASP .Net Core中輕松,簡單地使用Online Designer
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
FastReport .Net 2018的一個新穎之處是將OnlineDesigner改編為.Net Core框架。 一般來說,OnlineDesigner并沒有改變。今天將在ASP.Net Core項目中使用它的一些功能。 首先需要在Web設(shè)計器中構(gòu)建OnlineDesigner。 在OnlineDesigner的web designer中,應(yīng)該選擇必須存在的組件,樣式,插件等。請注意“配置”部分。需要在此處設(shè)置API URLs。選擇FastReport.Web for Core:

組裝完Designer后,即可下載存檔。 創(chuàng)建一個ASP.Net Core Web Application:

選擇Web Application(Model-View-Controller):

需要將FastReport.Web庫添加到項目中。可以使用Manage Nuget Packages執(zhí)行此操作。在解決方案資源管理器中右鍵單擊項目的根目錄,然后選擇Manage Nuget Packages:

在包管理器中選擇Local package source:

需要在文件夾上配置它:С:\ Program Files(x86)\ FastReports \ FastReport.Net \ Nugets。請單擊齒輪圖標:

之后,將看到FastReport包。安裝FastReport.Web:

開始創(chuàng)建程序。 需要將文件夾WebReportDesigner放在wwwroot中下載的檔案中。 將兩個文件夾添加到wwwroot:Reports和DesignedReports。 在第一個文件夾中,將放置Simple List.frx報告模板和nwind.xml數(shù)據(jù)庫。這兩個文件都可以在文件夾C:\ Program Files(x86)\ FastReports \ FastReport.Net \ Demos \ Reports中獲取。將報告從OnlineDesigner保存到DesignedReports文件夾。 打開控制器HomeController.cs。添加庫來使用:
using FRCoreOnlineDesigner.Models; using FastReport.Web; using System.IO; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting;
在HomeController構(gòu)造函數(shù)中,獲取了hostingEnvironment對象,從中學(xué)習了wwwroot文件夾的絕對路徑。
 public HomeController(IHostingEnvironment hostingEnvironment)
 {
 _hostingEnvironment = hostingEnvironment;
 }
編輯Index方法:
 public IActionResult Index()
 {
 WebReport WebReport = new WebReport(); // Create a Web Report Object
 WebReport.Width = "1000"; // Set the width of the report
 WebReport.Height = "1000"; // Set the height of the report
 string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to wwwroot folder
 //string contentRootPath = _hostingEnvironment.ContentRootPath;
 
 WebReport.Report.Load(webRootPath + "/reports/Simple List.frx"); // Load the report into a WebReport object
 System.Data.DataSet dataSet = new System.Data.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
 
 WebReport.Mode = WebReportMode.Designer; // Set the mode of the web report object - display of the designer
 WebReport.DesignerPath = "/WebReportDesigner/index.html"; // Specify the URL of the online designer
 WebReport.DesignerSaveCallBack = "/Home/SaveDesignedReport"; // Set the view URL for the report save method
 
 ViewBag.WebReport = WebReport; // pass report to View
 return View();
 }
再添加一個方法-SaveDesignedReport方法:
[HttpPost]
 // call-back for save the designed report
 public ActionResult SaveDesignedReport(string reportID, string reportUUID)
 {
 string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to wwwroot folder ViewBag.Message = String.Format("Confirmed {0} {1}", reportID, reportUUID); // Set a message for view
 
 Stream reportForSave = Request.Body; // Write the result of the Post query to the stream
 string pathToSave = webRootPath + "/DesignedReports/TestReport.frx"; // get the path to save the file
 using (FileStream file = new FileStream(pathToSave, FileMode.Create)) // Create a file stream {
 reportForSave.CopyTo(file); // Save the result of the query to a file
 }
 return View();
 }
對于此Web方法,需要再添加一個Views。對于文件夾Views-> Home,調(diào)用上下文菜單并選擇Add-> New item。接下來,選擇MVC View Page模板和SaveDesignedReport。

頁面代碼非常簡單:
<h2>@ViewBag.Message</h2>
編輯view index.cshtml:
@{
 ViewData["Title"] = "Home Page";
}
@await ViewBag.WebReport.Render()
await語句指示對異步報告處理方法的調(diào)用。 現(xiàn)在打開Startup.cs類。 在Configure方法中,最開始添加以下行:
app.UseFastReport();
現(xiàn)在應(yīng)用程序?qū)⑹褂肍RCore庫。 啟動Web Application。

保存報表:

保存完成后,檢查DesignedReports文件夾:

該報告已成功保存,總結(jié)一下。FastReport .Net 2018已完成其Web組件對ASP .Net Core框架的全局調(diào)整。與此同時,開發(fā)人員試圖盡可能地保持使用其組件的機制,類似于它們在ASP .Net MVC中的使用。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn