如何在PHP应用程序中使用FastReport报表|第2部分

在本文的第1部分中,我们创建了一个ASP.Net Core应用程序,该应用程序中实现了以下方法:显示报表、显示报表设计器以及将设计器中更改的报表保存到服务器。但是,这些只是我们所说的两个功能。我们需要实现以pdf或html格式导出所需报表的方法,然后下载此报表。为了让用户知道哪些报表可供下载,我们将实现一种获取报表列表的方法。

本文的第1部分中,我们创建了一个ASP.Net Core应用程序,该应用程序中实现了以下方法:显示报表、显示报表设计器以及将设计器中更改的报表保存到服务器。但是,这些只是我们所说的两个功能。我们需要实现以pdf或html格式导出所需报表的方法,然后下载此报表。为了让用户知道哪些报表可供下载,我们将实现一种获取报表列表的方法。

将报表列表的数据结构添加到数据的模型(“Model”文件夹):

 public class Reports { // Report ID public int Id { get; set; } // Report File Name public string ReportName { get; set; } }

现在,我们可以在控制器中创建带有标识符的报表列表:

 // Fill in the list of reports Reports[] reportItems = new Reports[] { new Reports { Id = 1, ReportName = "Master-Detail.frx" }, new Reports { Id = 2, ReportName = "Matrix.frx" } };

如上所述,我们需要客户端应用程序中的报表列表。这对于显示报表,在设计器中编辑报表以及下载pdf或html格式的报表很有用。

 // Get a list of reports in json format [HttpGet] public IEnumerable Get() { return reportItems; // Gets a list of reports }

这里的一切都很简单——json格式的报表列表。现在,我们正在实现一种相当复杂的获取报表导出的方法:

// Get the report file in pdf / html // Attribute has a required id parameter [HttpGet("{id}")] public IActionResult Get(int id, [FromQuery] ReportQuery query) { string mime = "application/" + query.Format; // MIME header with default value // Find the report Reports reportItem = reportItems.FirstOrDefault((p) => p.Id == id); // Get the value of the collection by identifier if (reportItem != null) { string reportPath = (webRoot + "/App_Data/" + reportItem.ReportName); // Determine the path to the report string dataPath = (webRoot + "/App_Data/nwind.xml");// Determine the path to the database using (MemoryStream stream = new MemoryStream()) // Create a stream for the report { try { using (DataSet dataSet = new DataSet()) { // Fill the source with data dataSet.ReadXml(dataPath); // Turn on FastReport web mode Config.WebMode = true; using (Report report = new Report()) { report.Load(reportPath); // Download report report.RegisterData(dataSet, "NorthWind"); // We register data in the report if (query.Parameter != null) { report.SetParameterValue("Parameter", query.Parameter); // Set the value of the report parameter, if the parameter value is transmitted in the URL } report.Prepare();//prepare a report // if pdf format is selected if (query.Format == "pdf") { // Export report to PDF PDFExport pdf = new PDFExport(); // We use a stream to store the report so as not to create extra files report.Export(pdf, stream); } // if html report format is selected else if (query.Format == "html") { // Export report to HTML HTMLExport html = new HTMLExport(); html.SinglePage = true; // Single page report html.Navigator = false; // Top navigation bar html.EmbedPictures = true; // Embeds images in a document report.Export(html, stream); mime = "text/" + query.Format; // Redefine mime for html } } } // Get the name of the resulting report file with the desired extension var file = String.Concat(Path.GetFileNameWithoutExtension(reportPath), ".", query.Format); // Download the report file return File(stream.ToArray(), mime, file); } // Handle exceptions catch { return new NoContentResult(); } finally { stream.Dispose(); } } } else return NotFound(); }

此方法接收两个输入参数:id和query。第一个是我们列表中的报表标识符,第二个是我们在url中指定的报表参数集。我们将在下面查看此设置,但现在让我们继续使用Get方法。

如果需要,SetParameterValue方法设置我们传递给url的参数的值。这只是可能性的证明。

如您所见,从format参数中我们可以找到用户选择的内容。在我们的情况下,可以是pdf或html。根据格式,将导出报表。每种类型的导出都有自己的设置。结果,导出被保存到stream流中,然后被转换为文件以供下载。

现在回到Get-query方法的第二个参数。它具有一种ReportQuery。在数据模型中创建此类:

 // Report Request Structure public class ReportQuery { // Format of resulting report: pdf, html public string Format { get; set; } // Value of "Parameter" variable in report public string Parameter { get; set; } }

如您所知,Get方法是从WebAPI提取的。这个项目是一个混合项目。它同时具有View部分和WebAPI接口。

因此,为了获得显示报表的视图,必须形成以下形式的URL:

 https://localhost:44346/api/reports/ShowReportame=ReportName.frx

为了与报表设计器进行演示,链接仅需在方法名称上有所不同:

 https://localhost:44346/api/reports/Designerame=ReportName.frx

但是要获取报表的名称,您需要在客户端应用程序中提供可用报表的列表。您可以使用url获取json格式的报表列表:

 https://localhost:44346/api/reports/

要以选定的格式下载报表,您需要形成以下形式的网址:

 https://localhost:44346/api/reports/1ormat=pdf

如果报表编号对应于服务器上报表列表中的ID,则格式可能具有pdf或html值。此外,如果报表模板中有任何参数,则可以指定一个报表参数。然后该网址将如下所示:

 https://localhost:44346/api/reports/1ormat=pdfeter=REPORT

在这种情况下,报表中的参数本身应称为Parameter,其值取自url。通过类推,您可以在url中考虑任意多个参数。

我们已经完成了服务器端的工作。在本文的第3部分,我们将探讨在PHP的客户端应用程序中使用所有这些方法。

点击查看第1部分。

点击查看第3部分。

产品介绍 | 下载试用 | 优惠活动 | 在线客服

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年9月13日
下一篇 2019年9月13日

相关推荐

发表回复

登录后才能评论