FastReport.Net是一款適用於Windows Forms, ASP.NET和MVC框架的功能齊全的報表分析解決方案。可用在Microsoft Visual Studio 2005到2015,支持.Net Framework 2.0到4.x。我下載了一個FastReport進行測試使用,這個報表功能還是很強大的。
對其中FastReport的一些功能,我編寫整理了一個小小的案例進行介紹,進行了簡單的測試記錄,希望對了解FastReport的使用有幫助。案例界面功能如下所示。

1、FastReport的漢化處理
默認安裝FastReport提供多語言的資源,我們可以在程序運行的時候指定對應的語言位置和文件即可。
實現中文化界面的代碼如下所示
string baseDir = Path.Combine(Application.StartupPath, "Examples/TestFastReport"); FastReport.Utils.Res.LocaleFolder = Path.Combine(baseDir, "L18N"); var file = FastReport.Utils.Res.LocaleFolder + @"Chinese (Simplified).frl"; FastReport.Utils.Res.LoadLocale(file);
這樣我們在運行界面后,就可以看到FastReport的漢化界面了。

2、FastReport打印預覽
默認加入FastReport的控件,包含幾個主要的界面控件,如下所示。

其中PeviewControl就是預覽報表的控件,而DesignerControl則是設計報表的控件,我們這里介紹PeviewControl控件用來顯示報表。
首先我們在一個空白的窗體界面上拖動一個PreviewControl進行報表的處理,如下界面所示。

剩下的就是如何展示報表內容了。
加載報表的設計文件代碼如下所示。
//加載報表設計文件 this.Report = new FastReport.Report(); var reportFile = Path.Combine(baseDir, "Report/Simple List.frx"); this.Report.Load(reportFile); this.Report.Preview = this.previewControl1;
而報表設計界面加載完畢后,還需要指定報表的數據源,以便整體渲染呈現報表的內容,實現的代碼如下所示。
DataSet ds = new DataSet(); var dataFile = Path.Combine(baseDir, "Report/nwind.xml"); ds.ReadXml(dataFile); Report.RegisterData(ds, "NorthWind"); Report.Prepare(); Report.ShowPrepared();
運行界面,就可以得到下面的報表界面效果。
整個報表支持很多其他類型的操作,如條形碼、二維碼、圖表、圖片等內容的展示,具體可以參考其官方案例的界面。



3、FastReport打印設計
上面介紹了 FastReport的PreviewControl,其設計控件DesignerControl的用法類似,不過這個控件是用來設計修改報表文件的,我們處理的代碼如下所示。
加載報表設計文件代碼如下。
this.Report = new FastReport.Report(); var reportFile = Path.Combine(baseDir, "Report/Simple List.frx"); this.Report.Load(reportFile);
如果報表需要加載數據進行顯示,那么需要加載報表數據。
DataSet ds = new DataSet(); var dataFile = Path.Combine(baseDir, "Report/nwind.xml"); ds.ReadXml(dataFile); Report.RegisterData(ds, "NorthWind"); this.designerControl1.Report = this.Report; Report.Prepare(); Report.Design();
運行界面,可以得到運行效果如下所示。

4、FastReport導出PDF
FastReport的另一個場景是可以不需要界面展示,直接通過設計文件,實現PDF文件的導出處理,實現界面代碼如下所示。
private void btnPDFReport_Click(object sender, EventArgs e) { Report report = new Report(); var reportFile = Path.Combine(baseDir, "Report/Simple List.frx"); report.Load(reportFile); //准備數據 DataSet ds = new DataSet(); var dataFile = Path.Combine(baseDir, "Report/nwind.xml"); ds.ReadXml(dataFile); report.RegisterData(ds, "NorthWind"); //運行報表 report.Prepare(); //導出PDF報表 var file = FileDialogHelper.SavePdf("result.pdf"); if (!string.IsNullOrEmpty(file)) { PDFExport export = new PDFExport(); report.Export(export, file); } report.Dispose(); if(File.Exists(file)) { Process.Start(file); } }
這個部分沒有報表展示,直接導出的PDF並存儲,如果需要打開則可以看到報表的PDF文件如下所示。

5、FastReport使用實體業務對象生成報表
在我的Winform開發框架里面,主要采用的數據都是實體類對象數據。FastReport報表里面除了標准的DataSet數據源外,肯定也會支持實體類數據,這種實體類的業務對象數據也是使用很廣泛的。
private void btnRunExisting_Click(object sender, EventArgs e) { // 創建報表並加載設計文件 Report report = new Report(); report.Load(Path.Combine(baseDir, "Report/report.frx")); //注冊業務對象數據 report.RegisterData(FBusinessObject, "Categories"); //運行報表 report.Show(); report.Dispose(); }
其中的數據對象初始化代碼如下所示。
private void CreateBusinessObject() { FBusinessObject = new List<Category>(); Category category = new Category("Beverages", "Soft drinks, coffees, teas, beers"); category.Products.Add(new Product("Chai", 18m)); category.Products.Add(new Product("Chang", 19m)); category.Products.Add(new Product("Ipoh coffee", 46m)); FBusinessObject.Add(category); category = new Category("Confections", "Desserts, candies, and sweet breads"); category.Products.Add(new Product("Chocolade", 12.75m)); category.Products.Add(new Product("Scottish Longbreads", 12.5m)); category.Products.Add(new Product("Tarte au sucre", 49.3m)); FBusinessObject.Add(category); category = new Category("Seafood", "Seaweed and fish"); category.Products.Add(new Product("Boston Crab Meat", 18.4m)); category.Products.Add(new Product("Red caviar", 15m)); FBusinessObject.Add(category); }
從上面我們可以看到,數據源是一個實體類集合的列表,從而展示如何使用這些數據源構造報表,運行界面效果如下所示。

FastReport的功能很強大,其設計文件是獨立的,因此可以對報表設計文件進行修改調整,從而實現客戶端的維護處理,它的功能也是很強大,支持在報表中添加文本、圖像、線條、形狀、語句、條形碼、矩陣、表格、RTF、選擇框等,列表報表、分組報表、主從報表、多列報表等內容。
在Winform開發中,我們也可以使用XtraReport報表和RDLC報表引擎,這方面可以參考我之前的隨筆文章《DevExpress的XtraReport和微軟RDLC報表的使用和對比》 和《會員管理系統的設計和開發(2)-- RDLC報表的設計及動態加載》進行了解,感謝大家對博客的支持。
