如何在PHP应用程序中使用FastReport报表|第1部分
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获取报表导出的方法。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线