翻譯|使用教程|編輯:王香|2018-11-22 09:42:20.000|閱讀 654 次
概述:在本文中,我們將創建一個用于從服務器接收FastReport報表的API。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在本文中,我們將創建一個用于從服務器接收FastReport報表的API。首先,讓我們定義API是什么。從字面上看,這個縮寫代表了應用程序的軟件界面。這意味著應用程序具有提供對其功能的訪問的接口。在Web應用程序的上下文中,API是一種Web服務,具有一組與后端(應用程序或數據庫)交互的方法。換句話說,系統對我們來說是一個黑盒子,只有Web方法允許我們使用它。
因此,我們確定Web Api是一個帶有一組Web方法的Web服務。這些可以是HTTP協議支持的四種基本CRUD功能。它們由請求實現:GET - 讀取,POST - 創建,PUT - 更改,DELETE - 刪除。
我們將在本文中創建此Web應用程序。而專為.Net Core框架設計的FastReport.Core將為我們生成報表。 創建一個ASP .Net Core Web應用程序。在向導的第二步中創建一個新項目,您可以選擇Web應用程序的類型 - Web API:

首先,我們需要使用Nuget包管理器連接庫。

要安裝FastReport.Core和FastReport.Web包,您需要選擇本地存儲庫。但是,它必須配置。在存儲選擇下拉列表旁邊有一個齒輪圖標。單擊它并查看設置窗口:

選擇“Local package source”并更改源的路徑。然后,單擊“Update”按鈕。現在,在包管理器中,我們可以安裝FastReport.Core和FastReport.Web。 在我們的演示中,我們將了解如何從服務器下載報表,如何在瀏覽器中查看報表以及如何將參數傳遞給報表。因此,我們將使用的主要實體是報表。讓我們將Reports類添加到數據模型中,該模型將包含兩個字段:Id - 報表標識符,ReportName - 報表名稱。
模型Reports.cs
public class Reports
{
// Report ID
public int Id { get; set; }
// Report File Name
public string ReportName { get; set; }
}
在Controllers包中,默認情況下有一個ValuesController.cs類。我們用它。 在使用部分,我們需要庫:
using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Hosting; using WebApiCore.Models; using System.IO; using System.Data; using FastReport.Utils; using FastReport; using FastReport.Export.Html; using FastReport.Export.Pdf;
用于接收報表的URL可能包含其他參數:報表文件的格式,內聯顯示的符號,傳遞給報表的參數。將包含必填字段的類添加到控制器:
public class ReportQuery
{
// Format of resulting report: png, pdf, html
public string Format { get; set; }
// Enable Inline preview in browser (generates "inline" or "attachment")
public bool Inline { get; set; }
// Value of "Parameter" variable in report
public string Parameter { get; set; }
}
我們需要兩個我們將演示的報表模板和一個數據庫。我們使用交付的報表:Master-Detail.frx和Barcodes.frx。第一個報表的nwind.xml數據庫也來自FR的交付。我們將這三個文件放在App_Data文件夾中,該文件夾將在wwwroot目錄中創建。

控制器類可以具有執行路由角色的Route屬性。這意味著此類僅接受來自指定路徑的請求。我將使用注釋來解釋代碼,以及方法之間的文本插入。
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment; // Use the web hosting environment interface to get the root path
public ValuesController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
// Create a collection of data of type Reports. Add two reports.
Reports[] reportItems = new Reports[]
{
new Reports { Id = 1, ReportName = "Master-Detail.frx" },
new Reports { Id = 2, ReportName = "Barcode.frx" }
};
在控制器中獲取數據的默認方法是Get。通常它有一個重載版本,就像我們的情況一樣。動作方法通常具有關聯屬性。該屬性可以具有參數。
[HttpGet]
public IEnumerable<Reports> Get()
{
return reportItems; // Returns a list of reports.
}
// Attribute has required id parameter
[HttpGet("{id}")]
public IActionResult Get(int id, [FromQuery] ReportQuery query)
{
string mime = "application/" + query.Format; // MIME header with default value
// Find report
Reports reportItem = reportItems.FirstOrDefault((p) => p.Id == id); // we get the value of the collection by id
if (reportItem != null)
{
string webRootPath = _hostingEnvironment.WebRootPath; // determine the path to the wwwroot folder
string reportPath = (webRootPath + "/App_Data/" + reportItem.ReportName); // determine the path to the report
string dataPath = (webRootPath + "/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 by data
dataSet.ReadXml(dataPath);
// Turn on web mode FastReport
Config.WebMode = true;
using (Report report = new Report())
{
report.Load(reportPath); // Download the report
report.RegisterData(dataSet, "NorthWind"); // 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 passed to the URL
}
report.Prepare();//Prepare the report
// If pdf format is selected
if (query.Format == "pdf")
{
// Export report to PDF
PDFExport pdf = new PDFExport();
// Use the stream to store the report, so as not to create unnecessary 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 into a document
report.Export(html, stream);
mime = "text/" + query.Format; // Override mime for html
}
}
}
// Get the name of the resulting report file with the necessary extension var file = String.Concat(Path.GetFileNameWithoutExtension(reportPath), ".", query.Format);
// If the inline parameter is true, then open the report in the browser
if (query.Inline)
return File(stream.ToArray(), mime);
else
// Otherwise download the report file
return File(stream.ToArray(), mime, file); // attachment
}
// Handle exceptions
catch
{
return new NoContentResult();
}
finally
{
stream.Dispose();
}
}
}
else
return NotFound();
}
現在我們需要一個網頁來顯示報表。當然,您可以不使用它并在地址欄中輸入URL。但是,使用現成的報表超鏈接將更加清晰。 讓我們創建一個簡單的html索引文檔。將它放在wwwroot文件夾的根目錄中。這是它的內容:
<!DOCTYPE html> <html> <head> <title>FastReport.Core Web Api</title> <meta charset="utf-8" /> </head> <body> <h1>FastReport.Core Web Api</h1> <hr /> <a href="/api/values/">List Of All Reports</a><br /> <a href="/api/values/1?format=pdf">Get First Report in PDF</a><br /> <a href="/api/values/1?format=html">Get First Report in HTML</a><br /> <a href="/api/values/1?format=pdf&inline=true">Get First Report in PDF inline</a><br /> <a href="/api/values/2?format=html&inline=true">Get Second Report in HTML inline</a><br /> <a href="/api/values/1?format=pdf&inline=true¶meter=REPORT">Get First Report in PDF inline with Parameter=REPORT</a><br /> <a href="/api/values/1?format=html&inline=true¶meter=REPORT">Get First Report in HTML inline with Parameter=REPORT</a><br /> </body> </html> </html>
我們添加了7個超鏈接。第一個允許您顯示可用報表的列表。第二個允許您下載PDF格式的索引為1的報表。第三個超鏈接允許您下載HTML格式的索引為1的報表。第四個 - 允許您在瀏覽器中以PDF格式打開索引為1的報表。第五個 - 允許您在瀏覽器中以HTML格式索引2打開報表。第六個 - 允許您在瀏覽器中以PDF格式打開索引為1的報表,并將參數值傳送給它。第七個 - 允許您在瀏覽器中打開HTML格式的索引為1的報表,并將參數值傳送給它。
當然,要在報表中顯示參數的值,您需要將其添加到報表和報表頁面。 但是,將index.html添加到根目錄是不夠的。為了在默認情況下打開此頁面,您需要在啟動設置中調整某些內容。打開Properties文件夾,即launchSettings.json文件。并刪除兩行:“launchUrl”:“api / values”。
要啟用對靜態頁面和FastReport庫的支持,請在Startup.cs中的Configure方法中添加幾行:
app.UseFastReport(); app.UseDefaultFiles(); app.UseStaticFiles();
啟動應用程序:

七個超鏈接的列表。讓我們點擊第一個:

報表列表以json格式顯示。在.Net Core中,json完全取代了xml。事實是如此簡潔和易懂。 單擊第二個鏈接。瀏覽器以pdf格式下載報表:

單擊第五個鏈接。瀏覽器將報表打開為html頁面。

單擊最后一個鏈接。報表也會在瀏覽器中打開,但請注意其標題。標題顯示傳遞的參數的值 - REPORT。

因此,我們學習了如何使用FastReport.Core創建.Net Core Web API應用程序。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn