原創|使用教程|編輯:我只采一朵|2017-12-08 13:29:06.000|閱讀 702 次
概述:FastReport.Net中的Web報表發展迅猛,它廣受歡迎,并且很符合現在開發潮流和趨勢。而最近它有了一個新的功能——標簽,即允許你在網頁報表工具欄上創建標簽。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
FastReport.Net中的Web報表發展迅猛,它廣受歡迎,并且很符合現在開發潮流和趨勢。而最近它有了一個新的功能——標簽,即允許你在網頁報表工具欄上創建標簽。這些標簽允許你在同一個窗口中打開其他報表。這樣的解決方案可以方便地輸出一系列相似主題或相關背景的報表。它看起來長這樣:
標簽呈現為按鈕樣式。當選擇標簽時,我們可以在同一個窗口中運行報表。也就是說,現在不需要每個報表都單獨顯示在不同的WebReport對象中。這將有助于節省頁面空間,并避免網站擁堵。
我們來看一個這個如何實現該功能的例子。我使用的是MVC web項目。
我們將FastReport庫添加到項目中:
· FastReport.dll;
· FastReport.Web.dll。
你可以在FastReport.Net應用程序的文件夾中找到它們。
在控制器中主頁創建:報表對象、數據源和標簽的實例??偠灾@里所有的邏輯。
然后對庫作出聲明:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using FastReport.Web; using System.Web.UI.WebControls;
對于Index方法,編寫下面的代碼:
public ActionResult Index() { string report_path = "C:\\Program Files (x86)\\FastReports\\FastReport.Net\\Demos\\Reports\\"; //Report path System.Data.DataSet dataSet = new System.Data.DataSet(); //Create DataSet instance dataSet.ReadXml(report_path + "nwind.xml"); //Read XML databse WebReport webReport = new WebReport(); //Create webReport instance webReport.Width = Unit.Percentage(100); //Set the webReport object width 100% webReport.Height = Unit.Percentage(100); //Set the webReport object heigh 100% webReport.SinglePage = true; //Enable SinglePage mode webReport.Report.RegisterData(dataSet, "NorthWind"); //Register data source in the webReport object webReport.Report.Load(report_path + "Simple List.frx"); //Load a report into the webReport object webReport.CurrentTab.Name = "Simple List"; //Set the current tab name Report report2 = new Report(); //Create a Report instance which will be displayed in the second tab report2.RegisterData(dataSet, "NorthWind"); //Register data source in the report object report2.Load(report_path + "Labels.frx"); //Load a report into the report object webReport.AddTab(report2, "Labels").Properties.SinglePage=true; //Add web tab in the webReport object. Pass as parameters report object and tab name. Enable SinglePage mode for the tab. Report report3 = new Report(); //Create a Report instance which will be displayed in the third tab report3.RegisterData(dataSet, "NorthWind");//Register data source in the report object report3.Load(report_path + "Master-Detail.frx");//Load a report into the report object webReport.AddTab(report3, "Master-Detail");//Add web tab in the webReport object. Pass as parameters report object and tab name. webReport.TabPosition = TabPosition.InsideToolbar;//Set the property TabPosition ViewBag.WebReport = webReport; //Set the ViewBag as webReport return View(); }
還有一個有意思的屬性:
webReport.ShowTabClos??eButton
如果將其設置為true,則標簽上將會顯示一個“X”,可以用來刪除標簽。
這個標簽在交互式報表中非常有用,標簽將被動態創建并包含詳細的報表。如果不需要某個報表,你就可以關閉它的標簽。然后,如有必要,你可以再次生成標簽。
上面我們講解了如何創建標簽,向他們發送報表。我們使用的方法是:
public ReportTab AddTab(Report report, string name);
我們將報表對象和標簽的名稱作為多個參數傳遞。然而,你也可以使用一個參數搞定:
public ReportTab AddTab(Report report);
我們只傳遞報表對象。標簽名稱將自動生成。這將會作為標簽編號。
你還可以將已建好的報表發送到web報表的標簽中:
public ReportTab AddTab(Report report, string name, bool reportDone);
在這里,我們提交一個報表、一個標簽名稱和一個屬性,指出報表是否應該預先建立。你可以將已經構建好的報表文件上傳到報表對象中,并將最后一個參數設置為true。然后報表將從指定的fpx文件中加載。
看起來差不多會是這個樣子:
Report report2 = new Report(); //Create a Report instance which will be displayed in the second tab report2.RegisterData(dataSet, "NorthWind"); //Register data source in the report object report2.Load(report_path + "Labels.frx"); //Load a report into the report object report2.Prepare();//Prepare the report string s = this.Server.MapPath("~/App_Data/Prepared.fpx");//Set the location to save prepared report report2.SavePrepared(s);//Save prepared report Report firstReport = new Report();//Create instance of Report object firstReport.LoadPrepared(s);//Upload prepared report to the Report object webReport.AddTab(firstReport, "First tab", true);//Add the tab to the WebReport toolbar
我展示了如何將準備好的報表保存到文件中,然后加載它并在“Web報表”標簽中使用它。
我們切換到視圖。在Views-> Home文件夾中,打開Index.cshtml文件。
所有的頁面代碼由以下四行組成:
@{ ViewBag.Title = "Home Page"; } @ViewBag.WebReport.GetHtml()
最后一行是報表輸出。主控制器會向頁面發送一個報表。
在視圖_Layout.cshtml(在Views - >Shared文件夾中)的初始化中為Web報表添加腳本:
<head> … @WebReportGlobals.Scripts() @WebReportGlobals.Styles() … </head>
編輯位于Views文件夾中的Web.config,添加命名空間:
<namespaces> … <add namespace="FastReport" /> <add namespace="FastReport.Web" /> </namespaces>
編輯位于項目根目錄中的Web.config,添加處理程序:
<handlers> … <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/> </handlers>
毫無疑問,在Web報表中添加標簽的新功能是非常實用的,并且是符合用戶需求的。Web報表的功能正在逐漸增加。在不久的將來,Web報表將變得絲毫不遜色于桌面(desktop)報表。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn