原創|使用教程|編輯:鄭恭琳|2019-10-17 15:07:28.370|閱讀 370 次
概述:在本文的第1部分中,我們創建了一個ASP.Net Core應用程序,該應用程序中實現了以下方法:顯示報表、顯示報表設計器以及將設計器中更改的報表保存到服務器。但是,這些只是我們所說的兩個功能。我們需要實現以pdf或html格式導出所需報表的方法,然后下載此報表。為了讓用戶知道哪些報表可供下載,我們將實現一種獲取報表列表的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在本文的第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:
為了與報表設計器進行演示,鏈接僅需在方法名稱上有所不同:
但是要獲取報表的名稱,您需要在客戶端應用程序中提供可用報表的列表。您可以使用url獲取json格式的報表列表:
要以選定的格式下載報表,您需要形成以下形式的網址:
如果報表編號對應于服務器上報表列表中的ID,則格式可能具有pdf或html值。此外,如果報表模板中有任何參數,則可以指定一個報表參數。然后該網址將如下所示:
在這種情況下,報表中的參數本身應稱為Parameter,其值取自url。通過類推,您可以在url中考慮任意多個參數。
我們已經完成了服務器端的工作。在本文的第3部分,我們將探討在PHP的客戶端應用程序中使用所有這些方法。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn