.NET使用報表工具FastReport實現(xiàn)打印功能
FastReport是功能非常強大的報表工具,在本篇文章中講解如何使用FastReport實現(xiàn)打印功能。
一、新建一個窗體程序,窗體上面有設計界面和預覽界面兩個按鈕,分別對應FastReport的設計和預覽功能,其實現(xiàn)代碼如下:
using FastReport; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Dapper; namespace FastReportDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_Design_Click(object sender, EventArgs e) { // 顯示設計界面 CreateReport(true); } private void CreateReport(bool tfDesigin) { // 獲得當前程序的運行路徑 string path = Application.StartupPath; // 定義報表 Report report = new Report(); string strDirectory = path + "\\ReportFiles"; // 判斷文件路徑是否存在,不存在則創(chuàng)建文件夾 if (!Directory.Exists(strDirectory)) { // 不存在就創(chuàng)建目錄 Directory.CreateDirectory(strDirectory); } // 判斷文件是否存在 if (!File.Exists(strDirectory + "\\產(chǎn)品明細.frx")) { report.FileName = strDirectory + "\\產(chǎn)品明細.frx"; } else { report.Load(strDirectory + "\\產(chǎn)品明細.frx"); } // 創(chuàng)建報表文件的數(shù)據(jù)源 DataSet ds = new DataSet(); DataTable dt = GetDataSource(); DataTable dtSource = dt.Copy(); dtSource.TableName = "ProductDetail"; ds.Tables.Add(dtSource); report.RegisterData(ds); if (tfDesigin) { // 打開設計界面 report.Design(); } else { // 打開預覽界面 report.Show(); } } private DataTable GetDataSource() { DataTable dt = new DataTable(); // 數(shù)據(jù)庫連接 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"; SqlConnection conn = new SqlConnection(strCon); string strSql = @"SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM ProductDetail p INNER JOIN Category c ON p.CategoryId=c.CategoryId"; // 使用Dapper獲取數(shù)據(jù) IDataReader reader = conn.ExecuteReader(strSql); dt.Load(reader); return dt; } private void btn_Show_Click(object sender, EventArgs e) { // 顯示預覽界面 CreateReport(false); } } }
二、運行程序,點擊設計界面,打開FastReport的設計界面:
三、選擇數(shù)據(jù)源
在設計之前要先選擇數(shù)據(jù)源,只有選擇了數(shù)據(jù)源,報表文件才會有數(shù)據(jù)。
1、在Data文件夾下面選擇“Choose Report Data”選項:
2、在Choose Report Data界面選擇程序中要用到的數(shù)據(jù)源:
3、點擊“OK”按鈕以后,在設計界面的右側會顯示選擇的數(shù)據(jù)源:
四、報表的整體結構:
五、設計報表標題
1、設計報表標題要使用到“A”控件:
2、將左側的"A"控件拖拽到報表標題區(qū)域:
3、設置標題:
雙擊報表標題區(qū)域的A控件,即可打開輸入標題的界面:
4、輸入報表標題,點擊“OK”按鈕:
報表標題區(qū)域就會顯示設計的標題,并可以設置標題的對齊方式。
六:設計報表數(shù)據(jù)區(qū)域
1、設計報表數(shù)據(jù),要使用到表格控件,表格控件如下圖所示:
2、將表格拖拽到數(shù)據(jù)區(qū)域,設計表格要顯示的行數(shù)和列數(shù):
3、表格顯示的內容:
4、表格界面:
七、設置表格事件
給表格添加數(shù)據(jù)綁定事件:
設置了事件以后,雙擊事件即可進入代碼編輯界面,綁定事件的代碼如下:
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) { // 設置數(shù)據(jù)源 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(); } } } }
八、頁腳顯示打印時間:
九、保存報表格式
設計完報表格式以后,一定要記得保存:
十、效果
因為界面太大,所以分了兩個截圖顯示:
到此這篇關于.NET使用報表工具FastReport實現(xiàn)打印功能的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
EntityFramework 6.x學習之多個上下文遷移實現(xiàn)分布式事務詳解
這篇文章主要給大家介紹了關于EntityFramework 6.x學習之多個上下文遷移實現(xiàn)分布式事務的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們隨著小編來一起學習學習吧。2017-10-10基于?.NET?6?的ASP.NET?Core啟動地址配置方法及優(yōu)先級順序
這篇文章主要介紹了ASP.NET?Core啟動地址配置方法及優(yōu)先級順序,?.NET?6?使用了最小?WEB?API,?配置方式已經(jīng)部分發(fā)生了變化,下面文章我們來看看具體的方法,需要的小伙伴可以參考一下2022-03-03asp.net+ajax+sqlserver自動補全功能實現(xiàn)解析
這篇文章主要介紹了asp.net + ajax + sqlserver 自動補全功能,需要的朋友可以參考下2014-03-03ASP.NET?Core?6框架揭秘實例演示之如何承載你的后臺服務
這篇文章主要介紹了ASP.NET?Core?6框架揭秘實例演示之如何承載你的后臺服務,主要包括利用承載服務收集性能指標、依賴注入的應用、配置選項的應用等知識點,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-03-03