首頁>技術>

建立報表時,通常需要從外部傳輸一些值。這是為了將資料過濾到報表中,或管理報表的邏輯。在使用FastReport.Net的實踐中,我經常遇到這種需求。由於我主要使用Web報表,因此我通過url將引數傳遞到報表中。通常,我的報表非常複雜,並且不僅限於一個引數。因此,需要傳遞引數列表,即key值集列表。key是設定的名稱。

下面我們可以一起實踐操作一下。在這種情況下,我使用ASP. Ne Core Web Api應用程式。

namespace ParametersWeb.Models{ public class Reports { // Report ID public int Id { get; set; } // Report File Name public string ReportName { get; set; } }}

ValuesController:

填寫報表陣列:

Reports[] reportItems = new Reports[] { new Reports { Id = 1, ReportName = "Parameters.frx" }, new Reports { Id = 2, ReportName = "Master-Detail.frx" }  };

報表的生成方法是非同步的,因為它使用非同步方法將html格式的報表轉換。如您所知,這種格式我們希望在瀏覽器中顯示報表:

[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(); }

此方法的邏輯本質如下:我們上傳選定的報表模板,從url解析引數並將其值傳輸到報表。然後,我們以html格式轉換報表,然後將檔案返回給客戶端。

您傳遞給報表的引數名稱應與報表中的引數明確匹配:

FastReport.Net中文教程:如何將選項列表傳輸到Web報表

重要通知:FastReport助力醫療機構,價值3000+的.NET正版授許可權時限量免費申請!趕快點選“了解更多”搶佔名額!

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Django簡介、ORM、核心模組