FastReport.Net中文教程:如何將選項(xiàng)列表傳輸?shù)絎eb報(bào)表
創(chuàng)建報(bào)表時(shí),通常需要從外部傳輸一些值。這是為了將數(shù)據(jù)過(guò)濾到報(bào)表中,或管理報(bào)表的邏輯。在使用FastReport.Net的實(shí)踐中,我經(jīng)常遇到這種需求。由于我主要使用Web報(bào)表,因此我通過(guò)url將參數(shù)傳遞到報(bào)表中。通常,我的報(bào)表非常復(fù)雜,并且不僅限于一個(gè)參數(shù)。因此,需要傳遞參數(shù)列表,即key值集列表。key是設(shè)置的名稱。
下面我們可以一起實(shí)踐操作一下。在這種情況下,我使用ASP. Ne Core Web Api應(yīng)用程序。
namespace ParametersWeb.Models { public class Reports { // Report ID public int Id { get; set; } // Report File Name public string ReportName { get; set; } } }
ValuesController:
填寫報(bào)表數(shù)組:
Reports[] reportItems = new Reports[] { new Reports { Id = 1, ReportName = "Parameters.frx" }, new Reports { Id = 2, ReportName = "Master-Detail.frx" } };
報(bào)表的生成方法是異步的,因?yàn)樗褂卯惒椒椒▽tml格式的報(bào)表轉(zhuǎn)換。如您所知,這種格式我們希望在瀏覽器中顯示報(bào)表:
[HttpGet("{id}")] public async System.Threading.Tasks.TaskGetAsync(int id) { string mime = "application/html"; // MIME header with default value // Find report var parameters = HttpContext.Request.QueryString.ToString().Substring(1); 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; WebReport webReport = new WebReport();//create the report object webReport.Report.Load(reportPath); //upload the report webReport.Report.RegisterData(dataSet, "NorthWind"); //register the data sourcw in the report if (parameters != null) { string[] parameterList = parameters.Split(','); foreach (string item in parameterList) { string[] parameter = item.Split('='); webReport.Report.SetParameterValue(parameter[0], parameter[1]); //set the report parameter value } } // inline registration of FastReport javascript webReport.Inline = true;//allow to register scripts and styles in HTML-body intead of putting them in the header HtmlString reportHtml = await webReport.Render(); //upload the report in HTML byte[] streamArray = Encoding.UTF8.GetBytes(reportHtml.ToString()); stream.Write(streamArray, 0, streamArray.Length);//write down the report in the stream } // Get the name of the resulting report file with the necessary extension var file = String.Concat(Path.GetFileNameWithoutExtension(reportPath), ".", "html"); return File(stream.ToArray(), mime, file); // attachment } // Handle exceptions catch { return new NoContentResult(); } finally { stream.Dispose(); } } } else return NotFound(); }
此方法的邏輯本質(zhì)如下:我們上傳選定的報(bào)表模板,從url解析參數(shù)并將其值傳輸?shù)綀?bào)表。然后,我們以html格式轉(zhuǎn)換報(bào)表,然后將文件返回給客戶端。
您傳遞給報(bào)表的參數(shù)名稱應(yīng)與報(bào)表中的參數(shù)明確匹配:
掃碼立即申請(qǐng)