Use FastReport to create a report in Winform development.
FastReport. Net is a fully functional Report Analysis Solution for Windows Forms, ASP. NET and MVC frameworks. Available in Microsoft Visual Studio 2005 to 2015, supports. Net Framework 2.0 to 4.x. I downloaded a FastReport and used it for testing. This report is still very powerful.
I have compiled a small case to introduce some FastReport functions and made a simple test record, hoping to help you understand the use of FastReport. The function of the Case interface is as follows.
1. Localization of FastReport
FastReport is installed by default to provide multi-language resources. You can specify the corresponding language location and file when running the program.
The code for Implementing the Cultural interface is as follows:
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);
In this way, we can see the FastReport Chinese interface after running the interface.
2. FastReport print preview
By default, the FastReport control is added, including several main interface controls, as shown below.
Here, PeviewControl is the control for previewing reports, while DesignerControl is the control for designing reports. Here we will introduce the PeviewControl Control for displaying reports.
First, we drag a PreviewControl on a blank form interface to process the report, as shown in the following interface.
The rest is how to display the report content.
The code for loading the report design file is as follows.
// Load the report design file this. report = new FastReport. report (); var reportFile = Path. combine (baseDir, "Report/Simple List. frx "); this. report. load (reportFile); this. report. preview = this. previewControl1;
After the report design interface is loaded, you must specify the report data source to render the report content as a whole. The implementation code is as follows.
DataSet ds = new DataSet(); var dataFile = Path.Combine(baseDir, "Report/nwind.xml"); ds.ReadXml(dataFile); Report.RegisterData(ds, "NorthWind"); Report.Prepare(); Report.ShowPrepared();
The following report page is displayed.
The entire report supports many other types of operations, such as bar code, QR code, charts, and images. For details, refer to the official case interface.
3. FastReport printing design
The PreviewControl of FastReport is introduced above. Its Design Control DesignerControl is similar in usage. However, this control is used to design and modify report files. The code we process is as follows.
The code for loading the report design file is as follows.
this.Report = new FastReport.Report(); var reportFile = Path.Combine(baseDir, "Report/Simple List.frx"); this.Report.Load(reportFile);
If the report needs to be loaded for display, You need to load the report data.
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();
The running interface shows the running effect as follows.
4. FastReport export PDF
Another scenario of FastReport is that you can export PDF files directly through the design file without interface display. The interface code is as follows.
Private void btnPDFReport_Click (object sender, EventArgs e) {Report report = new Report (); var reportFile = Path. combine (baseDir, "Report/Simple List. frx "); report. load (reportFile); // prepare the data DataSet ds = new DataSet (); var dataFile = Path. combine (baseDir, "Report/nwind. xml "); ds. readXml (dataFile); report. registerData (ds, "NorthWind"); // run the report. prepare (); // export the PDF report var file = FileDialogHelper. sa VePdf ("result.pdf .pdf"); if (! String. isNullOrEmpty (file) {export = new export (); report. export (export, file);} report. dispose (); if (File. exists (file) {Process. start (file );}}
This section does not show reports. The exported PDF files are saved. If you want to open the PDF file, you can see the following PDF file of the report.
5. FastReport uses Entity business objects to Generate Reports
In my Winform development framework, the main data used is entity-Class Object Data. In addition to the Standard DataSet data source, the FastReport report certainly supports entity-type data. This entity-type business object data is also widely used.
Private void btnRunExisting_Click (object sender, EventArgs e) {// create a Report and load the design file report = new Report (); report. load (Path. combine (baseDir, "Report/report. frx "); // register the business object data report. registerData (FBusinessObject, "Categories"); // run the report. show (); report. dispose ();}
The data object initialization code is as follows.
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); }
From the above we can see that the data source is a list of entity classes, which shows how to use these data sources to construct reports. The running interface is as follows.
FastReport has powerful functions and its design file is independent. Therefore, you can modify and adjust the report design file to implement maintenance and processing on the client. It also has powerful functions, you can add text, images, lines, shapes, statements, bar codes, matrices, tables, RTF, and selection boxes to reports, including list reports, grouped reports, master-slave reports, and multi-column reports.
In Winform development, we can also use XtraReport reports and RDLC report engines, in this regard, you can refer to my previous article "use and comparison of DevExpress XtraReport and Microsoft RDLC reports" and "design and development of the member Management System (2) -- RDLC report design and dynamic loading. Thank you for your support for the blog.