翻譯|使用教程|編輯:王香|2018-11-22 09:42:20.000|閱讀 642 次
概述:在本文中,我們將創(chuàng)建一個(gè)用于從服務(wù)器接收FastReport報(bào)表的API。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
在本文中,我們將創(chuàng)建一個(gè)用于從服務(wù)器接收FastReport報(bào)表的API。首先,讓我們定義API是什么。從字面上看,這個(gè)縮寫(xiě)代表了應(yīng)用程序的軟件界面。這意味著應(yīng)用程序具有提供對(duì)其功能的訪(fǎng)問(wèn)的接口。在Web應(yīng)用程序的上下文中,API是一種Web服務(wù),具有一組與后端(應(yīng)用程序或數(shù)據(jù)庫(kù))交互的方法。換句話(huà)說(shuō),系統(tǒng)對(duì)我們來(lái)說(shuō)是一個(gè)黑盒子,只有Web方法允許我們使用它。
因此,我們確定Web Api是一個(gè)帶有一組Web方法的Web服務(wù)。這些可以是HTTP協(xié)議支持的四種基本CRUD功能。它們由請(qǐng)求實(shí)現(xiàn):GET - 讀取,POST - 創(chuàng)建,PUT - 更改,DELETE - 刪除。
我們將在本文中創(chuàng)建此Web應(yīng)用程序。而專(zhuān)為.Net Core框架設(shè)計(jì)的FastReport.Core將為我們生成報(bào)表。 創(chuàng)建一個(gè)ASP .Net Core Web應(yīng)用程序。在向?qū)У牡诙街袆?chuàng)建一個(gè)新項(xiàng)目,您可以選擇Web應(yīng)用程序的類(lèi)型 - Web API:
首先,我們需要使用Nuget包管理器連接庫(kù)。
要安裝FastReport.Core和FastReport.Web包,您需要選擇本地存儲(chǔ)庫(kù)。但是,它必須配置。在存儲(chǔ)選擇下拉列表旁邊有一個(gè)齒輪圖標(biāo)。單擊它并查看設(shè)置窗口:
選擇“Local package source”并更改源的路徑。然后,單擊“Update”按鈕。現(xiàn)在,在包管理器中,我們可以安裝FastReport.Core和FastReport.Web。 在我們的演示中,我們將了解如何從服務(wù)器下載報(bào)表,如何在瀏覽器中查看報(bào)表以及如何將參數(shù)傳遞給報(bào)表。因此,我們將使用的主要實(shí)體是報(bào)表。讓我們將Reports類(lèi)添加到數(shù)據(jù)模型中,該模型將包含兩個(gè)字段:Id - 報(bào)表標(biāo)識(shí)符,ReportName - 報(bào)表名稱(chēng)。
模型Reports.cs
public class Reports { // Report ID public int Id { get; set; } // Report File Name public string ReportName { get; set; } }
在Controllers包中,默認(rèn)情況下有一個(gè)ValuesController.cs類(lèi)。我們用它。 在使用部分,我們需要庫(kù):
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;
用于接收?qǐng)?bào)表的URL可能包含其他參數(shù):報(bào)表文件的格式,內(nèi)聯(lián)顯示的符號(hào),傳遞給報(bào)表的參數(shù)。將包含必填字段的類(lèi)添加到控制器:
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; } }
我們需要兩個(gè)我們將演示的報(bào)表模板和一個(gè)數(shù)據(jù)庫(kù)。我們使用交付的報(bào)表:Master-Detail.frx和Barcodes.frx。第一個(gè)報(bào)表的nwind.xml數(shù)據(jù)庫(kù)也來(lái)自FR的交付。我們將這三個(gè)文件放在App_Data文件夾中,該文件夾將在wwwroot目錄中創(chuàng)建。
控制器類(lèi)可以具有執(zhí)行路由角色的Route屬性。這意味著此類(lèi)僅接受來(lái)自指定路徑的請(qǐng)求。我將使用注釋來(lái)解釋代碼,以及方法之間的文本插入。
[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" } };
在控制器中獲取數(shù)據(jù)的默認(rèn)方法是Get。通常它有一個(gè)重載版本,就像我們的情況一樣。動(dòng)作方法通常具有關(guān)聯(lián)屬性。該屬性可以具有參數(shù)。
[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(); }
現(xiàn)在我們需要一個(gè)網(wǎng)頁(yè)來(lái)顯示報(bào)表。當(dāng)然,您可以不使用它并在地址欄中輸入U(xiǎn)RL。但是,使用現(xiàn)成的報(bào)表超鏈接將更加清晰。 讓我們創(chuàng)建一個(gè)簡(jiǎn)單的html索引文檔。將它放在wwwroot文件夾的根目錄中。這是它的內(nèi)容:
<!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個(gè)超鏈接。第一個(gè)允許您顯示可用報(bào)表的列表。第二個(gè)允許您下載PDF格式的索引為1的報(bào)表。第三個(gè)超鏈接允許您下載HTML格式的索引為1的報(bào)表。第四個(gè) - 允許您在瀏覽器中以PDF格式打開(kāi)索引為1的報(bào)表。第五個(gè) - 允許您在瀏覽器中以HTML格式索引2打開(kāi)報(bào)表。第六個(gè) - 允許您在瀏覽器中以PDF格式打開(kāi)索引為1的報(bào)表,并將參數(shù)值傳送給它。第七個(gè) - 允許您在瀏覽器中打開(kāi)HTML格式的索引為1的報(bào)表,并將參數(shù)值傳送給它。
當(dāng)然,要在報(bào)表中顯示參數(shù)的值,您需要將其添加到報(bào)表和報(bào)表頁(yè)面。 但是,將index.html添加到根目錄是不夠的。為了在默認(rèn)情況下打開(kāi)此頁(yè)面,您需要在啟動(dòng)設(shè)置中調(diào)整某些內(nèi)容。打開(kāi)Properties文件夾,即launchSettings.json文件。并刪除兩行:“launchUrl”:“api / values”。
要啟用對(duì)靜態(tài)頁(yè)面和FastReport庫(kù)的支持,請(qǐng)?jiān)赟tartup.cs中的Configure方法中添加幾行:
app.UseFastReport(); app.UseDefaultFiles(); app.UseStaticFiles();
啟動(dòng)應(yīng)用程序:
七個(gè)超鏈接的列表。讓我們點(diǎn)擊第一個(gè):
報(bào)表列表以json格式顯示。在.Net Core中,json完全取代了xml。事實(shí)是如此簡(jiǎn)潔和易懂。 單擊第二個(gè)鏈接。瀏覽器以pdf格式下載報(bào)表:
單擊第五個(gè)鏈接。瀏覽器將報(bào)表打開(kāi)為html頁(yè)面。
單擊最后一個(gè)鏈接。報(bào)表也會(huì)在瀏覽器中打開(kāi),但請(qǐng)注意其標(biāo)題。標(biāo)題顯示傳遞的參數(shù)的值 - REPORT。
因此,我們學(xué)習(xí)了如何使用FastReport.Core創(chuàng)建.Net Core Web API應(yīng)用程序。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn