如何在PHP應(yīng)用程序中使用FastReport報(bào)表|第1部分
FastReport.Net是專門為.net平臺(tái)創(chuàng)建的。因此,Web報(bào)表可以使用ASP.Net和ASP.Net Core技術(shù)。
但是,萬(wàn)維網(wǎng)上的大多數(shù)網(wǎng)站仍然是用PHP編寫的。許多人希望在其php應(yīng)用程序中顯示FastReport報(bào)表。如您所知,這可以歸功于http協(xié)議。我們將只使用PHP應(yīng)用程序作為客戶端,使用ASP.Net Core作為服務(wù)器。
我們將提供兩種php和html格式之一的報(bào)表輸出,報(bào)表設(shè)計(jì)器輸出和報(bào)表下載(作為演示,兩種格式就足夠了)。
因此,在php應(yīng)用程序中將有三個(gè)頁(yè)面:顯示報(bào)表、顯示報(bào)表設(shè)計(jì)器、下載報(bào)表。
讓我們繼續(xù)服務(wù)器端的實(shí)現(xiàn)。最合適的技術(shù)選擇是ASP.Net Core,因?yàn)樗强缙脚_(tái)的,這意味著該應(yīng)用程序也可以在Linux服務(wù)器上運(yùn)行。據(jù)統(tǒng)計(jì),Linux服務(wù)器是用于托管網(wǎng)站的最受歡迎的解決方案。
首先,我們需要從開發(fā)人員的站點(diǎn)下載報(bào)表設(shè)計(jì)器(下載FastReport Online Designer試用版、下載FastReport.Net試用版)。要下載它,必須首先在特殊的配置器中進(jìn)行組裝。請(qǐng)注意設(shè)計(jì)器將在您的項(xiàng)目中使用的一種選擇。
您需要選擇FastReport.Web for Core。
因此,讓我們創(chuàng)建一個(gè)ASP.Net Core應(yīng)用程序。要在其中使用FastReport Web報(bào)表,您需要在NuGet管理器中安裝軟件包。這些程序包位于Nuget文件夾中的FastReport.Net安裝目錄中(小編已經(jīng)為您整理了安裝包,點(diǎn)擊這里下載)。因此,您將必須在NuGet程序包管理器中配置本地程序包源。
如此一來,您應(yīng)該安裝以下軟件包:FastReport.Core和FastReport.Web(點(diǎn)擊下載FastReport.Core,點(diǎn)擊下載FastReport.Web)。
要在項(xiàng)目中使用庫(kù),請(qǐng)將它們包含在Startup.cs文件中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { … app.UseFastReport(); … }
要在項(xiàng)目中使用庫(kù),請(qǐng)將它們包含在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)了一種獲取報(bào)表的方法。如果未傳遞報(bào)表名稱,則此方法應(yīng)獲取報(bào)表的名稱。對(duì)于此Web方法,您需要?jiǎng)?chuàng)建一個(gè)視圖。為此,請(qǐng)右鍵單擊該方法的簽名,然后從下拉菜單中選擇添加視圖“Add view ...”。接下來,只需單擊確定。
在創(chuàng)建的應(yīng)用程序中,將代碼替換為:
@await ViewBag.WebReport.Render()
我們具有報(bào)表的鏈接,但我們?nèi)晕磳?bào)表本身添加到項(xiàng)目中。創(chuàng)建一個(gè)App_Data文件夾并為其添加報(bào)表和數(shù)據(jù)庫(kù):
另外,在wwwroot中,我們將文件夾放置在報(bào)表設(shè)計(jì)器中:
現(xiàn)在,我們可以將報(bào)表設(shè)計(jì)器顯示方法添加到我們的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(); }
此方法還接收?qǐng)?bào)表名稱作為參數(shù)。為了顯示設(shè)計(jì)器,使用了WebReport對(duì)象。這里的重點(diǎn)是為報(bào)表保存事件的設(shè)計(jì)器和處理程序設(shè)置正確的路徑。
使用簡(jiǎn)單的代碼為此方法創(chuàng)建視圖:
@{ ViewData["Title"] = "Design"; } @await ViewBag.WebReport.Render()
向控制器添加另一個(gè)方法,以處理在設(shè)計(jì)器中編輯的報(bào)表的保存事件:
// 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(); }
請(qǐng)注意,由于我們?cè)谠O(shè)計(jì)器中打開報(bào)表名稱時(shí)會(huì)保存該報(bào)表,因此我們將報(bào)表以相同的名稱保存到App_Data文件夾中。因此,原始報(bào)表將被編輯的報(bào)表替換。
根據(jù)需要,如果執(zhí)行此方法沒有錯(cuò)誤,您將在設(shè)計(jì)器中保存該描述。
讓我們結(jié)束本文的第1部分。在第2部分中,我們將考慮一種通過url獲取報(bào)表導(dǎo)出的方法。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動(dòng) |