原創(chuàng)|使用教程|編輯:鄭恭琳|2019-10-17 14:53:22.100|閱讀 642 次
概述:FastReport.Net是專門為.net平臺創(chuàng)建的。因此,Web報表可以使用ASP.Net和ASP.Net Core技術(shù)。 但是,萬維網(wǎng)上的大多數(shù)網(wǎng)站仍然是用PHP編寫的。許多人希望在其php應(yīng)用程序中顯示FastReport報表。如您所知,這可以歸功于http協(xié)議。我們將只使用PHP應(yīng)用程序作為客戶端,使用ASP.Net Core作為服務(wù)器。 我們將提供兩種php和html格式之一的報表輸出,報表設(shè)計器輸出和報表下載(作為演示,兩種格式就足夠了)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
FastReport.Net是專門為.net平臺創(chuàng)建的。因此,Web報表可以使用ASP.Net和ASP.Net Core技術(shù)。
但是,萬維網(wǎng)上的大多數(shù)網(wǎng)站仍然是用PHP編寫的。許多人希望在其php應(yīng)用程序中顯示FastReport報表。如您所知,這可以歸功于http協(xié)議。我們將只使用PHP應(yīng)用程序作為客戶端,使用ASP.Net Core作為服務(wù)器。
我們將提供兩種php和html格式之一的報表輸出,報表設(shè)計器輸出和報表下載(作為演示,兩種格式就足夠了)。
因此,在php應(yīng)用程序中將有三個頁面:顯示報表、顯示報表設(shè)計器、下載報表。
讓我們繼續(xù)服務(wù)器端的實(shí)現(xiàn)。最合適的技術(shù)選擇是ASP.Net Core,因為它是跨平臺的,這意味著該應(yīng)用程序也可以在Linux服務(wù)器上運(yùn)行。據(jù)統(tǒng)計,Linux服務(wù)器是用于托管網(wǎng)站的最受歡迎的解決方案。
首先,我們需要從開發(fā)人員的站點(diǎn)下載報表設(shè)計器(下載FastReport Online Designer試用版、下載FastReport.Net試用版)。要下載它,必須首先在特殊的配置器中進(jìn)行組裝。請注意設(shè)計器將在您的項目中使用的一種選擇。
您需要選擇FastReport.Web for Core。
因此,讓我們創(chuàng)建一個ASP.Net Core應(yīng)用程序。要在其中使用FastReport Web報表,您需要在NuGet管理器中安裝軟件包。這些程序包位于Nuget文件夾中的FastReport.Net安裝目錄中(小編已經(jīng)為您整理了安裝包,點(diǎn)擊這里下載)。因此,您將必須在NuGet程序包管理器中配置本地程序包源。
如此一來,您應(yīng)該安裝以下軟件包:FastReport.Core和FastReport.Web(點(diǎn)擊下載FastReport.Core,點(diǎn)擊下載FastReport.Web)。
要在項目中使用庫,請將它們包含在Startup.cs文件中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { … app.UseFastReport(); … }
要在項目中使用庫,請將它們包含在Startup.cs文件中:
using FastReport.Web; using System.IO; using FastReport; using FastReport.Export.Html; using FastReport.Export.Pdf; using SimpleReportViewer.Models; using System.Data; using FastReport.Utils; namespace SimpleReportViewer.Controllers { [Route("api/[controller]")] public class ReportsController : Controller { private IHostingEnvironment _env; // Web application directory path public string webRoot { get { return _env.WebRootPath; } set { } } public ReportsController(IHostingEnvironment env) { _env = env; } // Show report by name [HttpGet("[action]")] public IActionResult ShowReport(string name) { if (name == null) name = "Master-Detail.frx"; WebReport WebReport = new WebReport(); WebReport.Width = "1000"; WebReport.Height = "1000"; WebReport.Report.Load(String.Format("{0}/App_Data/{1}", webRoot, name)); // Download the report to the WebReport object System.Data.DataSet dataSet = new System.Data.DataSet(); // Download the report to the WebReport object dataSet.ReadXml(String.Format("{0}/App_Data/nwind.xml", webRoot)); // Open the xml database WebReport.Report.RegisterData(dataSet, "NorthWind"); // Register the data source in the report ViewBag.WebReport = WebReport; // Pass the report to View return View(); } }
首先,我們獲得了wwwroot應(yīng)用程序文件夾的路徑。然后,我們實(shí)現(xiàn)了一種獲取報表的方法。如果未傳遞報表名稱,則此方法應(yīng)獲取報表的名稱。對于此Web方法,您需要創(chuàng)建一個視圖。為此,請右鍵單擊該方法的簽名,然后從下拉菜單中選擇添加視圖“Add view ...”。接下來,只需單擊確定。
在創(chuàng)建的應(yīng)用程序中,將代碼替換為:
@await ViewBag.WebReport.Render()
我們具有報表的鏈接,但我們?nèi)晕磳蟊肀旧硖砑拥巾椖恐小?chuàng)建一個App_Data文件夾并為其添加報表和數(shù)據(jù)庫:
另外,在wwwroot中,我們將文件夾放置在報表設(shè)計器中:
現(xiàn)在,我們可以將報表設(shè)計器顯示方法添加到我們的ReportsController中:
// Static variable for storing the report name public static string ReportName; // We show the designer with a report [HttpGet("[action]")] public IActionResult Design(string name) { if (name == null) name = "Master-Detail.frx"; var webRoot = _env.WebRootPath; WebReport WebReport = new WebReport(); WebReport.Width = "1000"; WebReport.Height = "1000"; WebReport.Report.Load(System.IO.Path.Combine(webRoot, (String.Format("App_Data/{0}", name)))); // Download the report to the WebReport object System.Data.DataSet dataSet = new System.Data.DataSet(); // Create a data source dataSet.ReadXml(System.IO.Path.Combine(webRoot, "App_Data/nwind.xml")); // Open the xml database WebReport.Report.RegisterData(dataSet, "NorthWind"); // Register the data source in the report ReportName = name; WebReport.Mode = WebReportMode.Designer; // Set the mode of the object web report - display designer WebReport.DesignerLocale = "en"; WebReport.DesignerPath = "/WebReportDesigner/index.html"; // Set the URL of the online designer WebReport.DesignerSaveCallBack = "api/reports/SaveDesignedReport"; // Set the view URL for the report save method WebReport.Debug = true; ViewBag.WebReport = WebReport; // Pass the report to View return View(); }
此方法還接收報表名稱作為參數(shù)。為了顯示設(shè)計器,使用了WebReport對象。這里的重點(diǎn)是為報表保存事件的設(shè)計器和處理程序設(shè)置正確的路徑。
使用簡單的代碼為此方法創(chuàng)建視圖:
@{ ViewData["Title"] = "Design"; } @await ViewBag.WebReport.Render()
向控制器添加另一個方法,以處理在設(shè)計器中編輯的報表的保存事件:
// call-back for save the designed report [HttpPost("[action]")] public IActionResult SaveDesignedReport(string reportID, string reportUUID) { var webRoot = _env.WebRootPath; ViewBag.Message = String.Format("Confirmed {0} {1}", reportID, reportUUID); // We set the message for presentation Stream reportForSave = Request.Body; // We write the result of the Post request to the stream string pathToSave = System.IO.Path.Combine(webRoot, @"App_Data/"+ ReportName); // We 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 request to a file } return View(); }
請注意,由于我們在設(shè)計器中打開報表名稱時會保存該報表,因此我們將報表以相同的名稱保存到App_Data文件夾中。因此,原始報表將被編輯的報表替換。
根據(jù)需要,如果執(zhí)行此方法沒有錯誤,您將在設(shè)計器中保存該描述。
讓我們結(jié)束本文的第1部分。在第2部分中,我們將考慮一種通過url獲取報表導(dǎo)出的方法。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動 |
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn