原創|使用教程|編輯:鄭恭琳|2020-04-20 13:45:17.237|閱讀 324 次
概述:創建報表時,通常需要從外部傳輸一些值。這是為了將數據過濾到報表中,或管理報表的邏輯。在使用FastReport.Net的實踐中,我經常遇到這種需求。由于我主要使用Web報表,因此我通過url將參數傳遞到報表中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
創建報表時,通常需要從外部傳輸一些值。這是為了將數據過濾到報表中,或管理報表的邏輯。在使用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格式轉換報表,然后將文件返回給客戶端。
您傳遞給報表的參數名稱應與報表中的參數明確匹配:

	 
	

	
	
	
	 掃碼立即申請
掃碼立即申請
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn