.NET中使用FastReport實現打印功能


FastReport是功能非常強大的報表工具,在本篇文章中講解如何使用FastReport實現打印功能。

一、新建一個窗體程序,窗體上面有設計界面和預覽界面兩個按鈕,分別對應FastReport的設計和預覽功能,其實現代碼如下:

 1 using FastReport;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 using System.Drawing;
 8 using System.IO;
 9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13 using Dapper;
14 
15 namespace FastReportDemo
16 {
17     public partial class Form1 : Form
18     {
19         public Form1()
20         {
21             InitializeComponent();
22         }
23 
24         private void btn_Design_Click(object sender, EventArgs e)
25         {
26             // 顯示設計界面
27             CreateReport(true);
28         }
29 
30 
31         private void CreateReport(bool tfDesigin)
32         {
33             // 獲得當前程序的運行路徑
34             string path = Application.StartupPath;
35             // 定義報表
36             Report report = new Report();
37             string strDirectory = path + "\\ReportFiles";
38 
39             // 判斷文件路徑是否存在,不存在則創建文件夾
40             if (!Directory.Exists(strDirectory))
41             {
42                 // 不存在就創建目錄
43                 Directory.CreateDirectory(strDirectory);
44             }
45 
46             // 判斷文件是否存在 
47             if (!File.Exists(strDirectory + "\\產品明細.frx"))
48             {
49                 report.FileName = strDirectory + "\\產品明細.frx";
50             }
51             else
52             {
53                 report.Load(strDirectory + "\\產品明細.frx");
54             }
55 
56             // 創建報表文件的數據源
57             DataSet ds = new DataSet();
58             DataTable dt = GetDataSource();
59             DataTable dtSource = dt.Copy();
60             dtSource.TableName = "ProductDetail";
61             ds.Tables.Add(dtSource);
62             report.RegisterData(ds);
63 
64             if (tfDesigin)
65             {
66                 // 打開設計界面
67                 report.Design();
68             }
69             else
70             {
71                 // 打開預覽界面
72                 report.Show();
73             }
74         }
75 
76         private DataTable GetDataSource()
77         {
78             DataTable dt = new DataTable();
79             // 數據庫連接
80             string strCon = @"Initial Catalog=StudentSystem;     Integrated Security=False;User Id=sa;Password=1qaz@WSX;Data Source=127.0.0.1;Failover Partner=127.0.0.1;Application Name=TransForCCT";
81             SqlConnection conn = new SqlConnection(strCon);
82             string strSql = @"SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM ProductDetail p INNER JOIN Category c
83                               ON p.CategoryId=c.CategoryId";
84             // 使用Dapper獲取數據
85             IDataReader reader = conn.ExecuteReader(strSql);
86             dt.Load(reader);
87             return dt;
88         }
89 
90         private void btn_Show_Click(object sender, EventArgs e)
91         {
92             // 顯示預覽界面
93             CreateReport(false);
94         }
95     }
96 }

二、運行程序,點擊設計界面,打開FastReport的設計界面:

 

三、選擇數據源

在設計之前要先選擇數據源,只有選擇了數據源,報表文件才會有數據。

1、在Data文件夾下面選擇“Choose Report Data”選項:

2、在Choose Report Data界面選擇程序中要用到的數據源:

3、點擊“OK”按鈕以后,在設計界面的右側會顯示選擇的數據源:

四、報表的整體結構:

五、設計報表標題

1、設計報表標題要使用到“A”控件:

2、將左側的"A"控件拖拽到報表標題區域:

3、設置標題:

雙擊報表標題區域的A控件,即可打開輸入標題的界面:

4、輸入報表標題,點擊“OK”按鈕:

報表標題區域就會顯示設計的標題,並可以設置標題的對齊方式。

六:設計報表數據區域

1、設計報表數據,要使用到表格控件,表格控件如下圖所示:

2、將表格拖拽到數據區域,設計表格要顯示的行數和列數:

3、表格顯示的內容:

4、表格界面:

七、設置表格事件

給表格添加數據綁定事件:

設置了事件以后,雙擊事件即可進入代碼編輯界面,綁定事件的代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport
{
  public class ReportScript
  {

    private void Table1_ManualBuild(object sender, EventArgs e)
    {
      // 設置數據源
      DataSourceBase rowData = Report.GetDataSource("ProductDetail"); 
      
      rowData.Init();
      
      Table1.PrintRow(0);
      Table1.PrintColumns();
      
      bool tfvar = false;
      while (rowData.HasMoreRows)
      {
        tfvar = true;
        Table1.PrintRow(1);
        Table1.PrintColumns();
        rowData.Next();
      }
      
      if (!tfvar)
      {
        Table1.PrintRow(2);
        Table1.PrintColumns();
      }
    }
  }
}

 

八、頁腳顯示打印時間:

九、保存報表格式

設計完報表格式以后,一定要記得保存:

十、效果

因為界面太大,所以分了兩個截圖顯示:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM