原創|使用教程|編輯:鄭恭琳|2019-10-17 14:53:22.100|閱讀 642 次
概述:FastReport.Net是專門為.net平臺創建的。因此,Web報表可以使用ASP.Net和ASP.Net Core技術。 但是,萬維網上的大多數網站仍然是用PHP編寫的。許多人希望在其php應用程序中顯示FastReport報表。如您所知,這可以歸功于http協議。我們將只使用PHP應用程序作為客戶端,使用ASP.Net Core作為服務器。 我們將提供兩種php和html格式之一的報表輸出,報表設計器輸出和報表下載(作為演示,兩種格式就足夠了)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
FastReport.Net是專門為.net平臺創建的。因此,Web報表可以使用ASP.Net和ASP.Net Core技術。
但是,萬維網上的大多數網站仍然是用PHP編寫的。許多人希望在其php應用程序中顯示FastReport報表。如您所知,這可以歸功于http協議。我們將只使用PHP應用程序作為客戶端,使用ASP.Net Core作為服務器。
我們將提供兩種php和html格式之一的報表輸出,報表設計器輸出和報表下載(作為演示,兩種格式就足夠了)。
因此,在php應用程序中將有三個頁面:顯示報表、顯示報表設計器、下載報表。
讓我們繼續服務器端的實現。最合適的技術選擇是ASP.Net Core,因為它是跨平臺的,這意味著該應用程序也可以在Linux服務器上運行。據統計,Linux服務器是用于托管網站的最受歡迎的解決方案。
首先,我們需要從開發人員的站點下載報表設計器(下載FastReport Online Designer試用版、下載FastReport.Net試用版)。要下載它,必須首先在特殊的配置器中進行組裝。請注意設計器將在您的項目中使用的一種選擇。
您需要選擇FastReport.Web for Core。
因此,讓我們創建一個ASP.Net Core應用程序。要在其中使用FastReport Web報表,您需要在NuGet管理器中安裝軟件包。這些程序包位于Nuget文件夾中的FastReport.Net安裝目錄中(小編已經為您整理了安裝包,點擊這里下載)。因此,您將必須在NuGet程序包管理器中配置本地程序包源。
如此一來,您應該安裝以下軟件包:FastReport.Core和FastReport.Web(點擊下載FastReport.Core,點擊下載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應用程序文件夾的路徑。然后,我們實現了一種獲取報表的方法。如果未傳遞報表名稱,則此方法應獲取報表的名稱。對于此Web方法,您需要創建一個視圖。為此,請右鍵單擊該方法的簽名,然后從下拉菜單中選擇添加視圖“Add view ...”。接下來,只需單擊確定。
在創建的應用程序中,將代碼替換為:
@await ViewBag.WebReport.Render()
我們具有報表的鏈接,但我們仍未將報表本身添加到項目中。創建一個App_Data文件夾并為其添加報表和數據庫:
另外,在wwwroot中,我們將文件夾放置在報表設計器中:
現在,我們可以將報表設計器顯示方法添加到我們的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();
}
此方法還接收報表名稱作為參數。為了顯示設計器,使用了WebReport對象。這里的重點是為報表保存事件的設計器和處理程序設置正確的路徑。
使用簡單的代碼為此方法創建視圖:
@{
ViewData["Title"] = "Design";
}
@await ViewBag.WebReport.Render()
向控制器添加另一個方法,以處理在設計器中編輯的報表的保存事件:
// 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();
}
請注意,由于我們在設計器中打開報表名稱時會保存該報表,因此我們將報表以相同的名稱保存到App_Data文件夾中。因此,原始報表將被編輯的報表替換。
根據需要,如果執行此方法沒有錯誤,您將在設計器中保存該描述。
讓我們結束本文的第1部分。在第2部分中,我們將考慮一種通過url獲取報表導出的方法。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn