原創(chuàng)|使用教程|編輯:鄭恭琳|2019-10-17 15:07:28.370|閱讀 370 次
概述:在本文的第1部分中,我們創(chuàng)建了一個(gè)ASP.Net Core應(yīng)用程序,該應(yīng)用程序中實(shí)現(xiàn)了以下方法:顯示報(bào)表、顯示報(bào)表設(shè)計(jì)器以及將設(shè)計(jì)器中更改的報(bào)表保存到服務(wù)器。但是,這些只是我們所說(shuō)的兩個(gè)功能。我們需要實(shí)現(xiàn)以pdf或html格式導(dǎo)出所需報(bào)表的方法,然后下載此報(bào)表。為了讓用戶知道哪些報(bào)表可供下載,我們將實(shí)現(xiàn)一種獲取報(bào)表列表的方法。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
在本文的第1部分中,我們創(chuàng)建了一個(gè)ASP.Net Core應(yīng)用程序,該應(yīng)用程序中實(shí)現(xiàn)了以下方法:顯示報(bào)表、顯示報(bào)表設(shè)計(jì)器以及將設(shè)計(jì)器中更改的報(bào)表保存到服務(wù)器。但是,這些只是我們所說(shuō)的兩個(gè)功能。我們需要實(shí)現(xiàn)以pdf或html格式導(dǎo)出所需報(bào)表的方法,然后下載此報(bào)表。為了讓用戶知道哪些報(bào)表可供下載,我們將實(shí)現(xiàn)一種獲取報(bào)表列表的方法。
將報(bào)表列表的數(shù)據(jù)結(jié)構(gòu)添加到數(shù)據(jù)的模型(“Model”文件夾):
public class Reports { // Report ID public int Id { get; set; } // Report File Name public string ReportName { get; set; } }
現(xiàn)在,我們可以在控制器中創(chuàng)建帶有標(biāo)識(shí)符的報(bào)表列表:
// 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" } };
如上所述,我們需要客戶端應(yīng)用程序中的報(bào)表列表。這對(duì)于顯示報(bào)表,在設(shè)計(jì)器中編輯報(bào)表以及下載pdf或html格式的報(bào)表很有用。
// Get a list of reports in json format [HttpGet] public IEnumerable Get() { return reportItems; // Gets a list of reports }
這里的一切都很簡(jiǎn)單——json格式的報(bào)表列表。現(xiàn)在,我們正在實(shí)現(xiàn)一種相當(dāng)復(fù)雜的獲取報(bào)表導(dǎo)出的方法:
// 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(); }
此方法接收兩個(gè)輸入?yún)?shù):id和query。第一個(gè)是我們列表中的報(bào)表標(biāo)識(shí)符,第二個(gè)是我們?cè)趗rl中指定的報(bào)表參數(shù)集。我們將在下面查看此設(shè)置,但現(xiàn)在讓我們繼續(xù)使用Get方法。
如果需要,SetParameterValue方法設(shè)置我們傳遞給url的參數(shù)的值。這只是可能性的證明。
如您所見(jiàn),從format參數(shù)中我們可以找到用戶選擇的內(nèi)容。在我們的情況下,可以是pdf或html。根據(jù)格式,將導(dǎo)出報(bào)表。每種類型的導(dǎo)出都有自己的設(shè)置。結(jié)果,導(dǎo)出被保存到stream流中,然后被轉(zhuǎn)換為文件以供下載。
現(xiàn)在回到Get-query方法的第二個(gè)參數(shù)。它具有一種ReportQuery。在數(shù)據(jù)模型中創(chuàng)建此類:
// 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提取的。這個(gè)項(xiàng)目是一個(gè)混合項(xiàng)目。它同時(shí)具有View部分和WebAPI接口。
因此,為了獲得顯示報(bào)表的視圖,必須形成以下形式的URL:
為了與報(bào)表設(shè)計(jì)器進(jìn)行演示,鏈接僅需在方法名稱上有所不同:
但是要獲取報(bào)表的名稱,您需要在客戶端應(yīng)用程序中提供可用報(bào)表的列表。您可以使用url獲取json格式的報(bào)表列表:
要以選定的格式下載報(bào)表,您需要形成以下形式的網(wǎng)址:
如果報(bào)表編號(hào)對(duì)應(yīng)于服務(wù)器上報(bào)表列表中的ID,則格式可能具有pdf或html值。此外,如果報(bào)表模板中有任何參數(shù),則可以指定一個(gè)報(bào)表參數(shù)。然后該網(wǎng)址將如下所示:
在這種情況下,報(bào)表中的參數(shù)本身應(yīng)稱為Parameter,其值取自u(píng)rl。通過(guò)類推,您可以在url中考慮任意多個(gè)參數(shù)。
我們已經(jīng)完成了服務(wù)器端的工作。在本文的第3部分,我們將探討在PHP的客戶端應(yīng)用程序中使用所有這些方法。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動(dòng) |
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn