在Winform開發中使用FastReport建立報表

FastReport.Net是一款適用於Windows Forms, ASP.NET和MVC框架的功能齊全的報表分析解決方案。可用在Microsoft Visual Studio 2005到2015,支持.Net Framework 2.0到4.x。我下載了一個FastReport進行測試使用,這個報表功能仍是很強大的。html

對其中FastReport的一些功能,我編寫整理了一個小小的案例進行介紹,進行了簡單的測試記錄,但願對了解FastReport的使用有幫助。案例界面功能以下所示。框架

一、FastReport的漢化處理

默認安裝FastReport提供多語言的資源,咱們能夠在程序運行的時候指定對應的語言位置和文件便可。post

實現中文化界面的代碼以下所示測試

            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的漢化界面了。this

 

二、FastReport打印預覽

默認加入FastReport的控件,包含幾個主要的界面控件,以下所示。url

其中PeviewControl就是預覽報表的控件,而DesignerControl則是設計報表的控件,咱們這裏介紹PeviewControl控件用來顯示報表。spa

首先咱們在一個空白的窗體界面上拖動一個PreviewControl進行報表的處理,以下界面所示。設計

剩下的就是如何展現報表內容了。3d

加載報表的設計文件代碼以下所示。code

            //加載報表設計文件
            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();

運行界面,就能夠獲得下面的報表界面效果。

 

整個報表支持不少其餘類型的操做,如條形碼、二維碼、圖表、圖片等內容的展現,具體能夠參考其官方案例的界面。

 

三、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();

運行界面,能夠獲得運行效果以下所示。

 

四、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文件以下所示。

 

 

五、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報表的設計及動態加載》進行了解,感謝你們對博客的支持。

相關文章
相關標籤/搜索