本文小編為大家詳細介紹“.NET中如何使用FastReport實現打印功能”,內容詳細,步驟清晰,細節處理妥當,希望這篇“.NET中如何使用FastReport實現打印功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
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";
// 判斷文件路徑是否存在,不存在則創建文件夾
if (!Directory.Exists(strDirectory))
{
// 不存在就創建目錄
Directory.CreateDirectory(strDirectory);
}
// 判斷文件是否存在
if (!File.Exists(strDirectory + "\\產品明細.frx"))
{
report.FileName = strDirectory + "\\產品明細.frx";
}
else
{
report.Load(strDirectory + "\\產品明細.frx");
}
// 創建報表文件的數據源
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();
// 數據庫連接
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獲取數據
IDataReader reader = conn.ExecuteReader(strSql);
dt.Load(reader);
return dt;
}
private void btn_Show_Click(object sender, EventArgs e)
{
// 顯示預覽界面
CreateReport(false);
}
}
}
在設計之前要先選擇數據源,只有選擇了數據源,報表文件才會有數據。






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

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





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

設置了事件以后,雙擊事件即可進入代碼編輯界面,綁定事件的代碼如下:
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();
}
}
}
}
設計完報表格式以后,一定要記得保存:

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


讀到這里,這篇“.NET中如何使用FastReport實現打印功能”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。