溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

發(fā)布時(shí)間:2022-03-09 16:08:10 來源:億速云 閱讀:775 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“.NET中如何使用FastReport實(shí)現(xiàn)打印功能”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“.NET中如何使用FastReport實(shí)現(xiàn)打印功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

一、新建一個(gè)窗體程序,窗體上面有設(shè)計(jì)界面和預(yù)覽界面兩個(gè)按鈕,分別對應(yīng)FastReport的設(shè)計(jì)和預(yù)覽功能,其實(shí)現(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)
        {
            // 顯示設(shè)計(jì)界面
            CreateReport(true);
        }


        private void CreateReport(bool tfDesigin)
        {
            // 獲得當(dāng)前程序的運(yùn)行路徑
            string path = Application.StartupPath;
            // 定義報(bào)表
            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)品明細(xì).frx"))
            {
                report.FileName = strDirectory + "\\產(chǎn)品明細(xì).frx";
            }
            else
            {
                report.Load(strDirectory + "\\產(chǎn)品明細(xì).frx");
            }

            // 創(chuàng)建報(bào)表文件的數(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)
            {
                // 打開設(shè)計(jì)界面
                report.Design();
            }
            else
            {
                // 打開預(yù)覽界面
                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)
        {
            // 顯示預(yù)覽界面
            CreateReport(false);
        }
    }
}

二、運(yùn)行程序,點(diǎn)擊設(shè)計(jì)界面,打開FastReport的設(shè)計(jì)界面:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

三、選擇數(shù)據(jù)源

在設(shè)計(jì)之前要先選擇數(shù)據(jù)源,只有選擇了數(shù)據(jù)源,報(bào)表文件才會有數(shù)據(jù)。

1、在Data文件夾下面選擇“Choose Report Data”選項(xiàng):

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

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

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

3、點(diǎn)擊“OK”按鈕以后,在設(shè)計(jì)界面的右側(cè)會顯示選擇的數(shù)據(jù)源:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

四、報(bào)表的整體結(jié)構(gòu):

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

五、設(shè)計(jì)報(bào)表標(biāo)題

1、設(shè)計(jì)報(bào)表標(biāo)題要使用到“A”控件:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

2、將左側(cè)的"A"控件拖拽到報(bào)表標(biāo)題區(qū)域:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

3、設(shè)置標(biāo)題:

雙擊報(bào)表標(biāo)題區(qū)域的A控件,即可打開輸入標(biāo)題的界面:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

4、輸入報(bào)表標(biāo)題,點(diǎn)擊“OK”按鈕:

報(bào)表標(biāo)題區(qū)域就會顯示設(shè)計(jì)的標(biāo)題,并可以設(shè)置標(biāo)題的對齊方式。

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

六:設(shè)計(jì)報(bào)表數(shù)據(jù)區(qū)域

1、設(shè)計(jì)報(bào)表數(shù)據(jù),要使用到表格控件,表格控件如下圖所示:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

2、將表格拖拽到數(shù)據(jù)區(qū)域,設(shè)計(jì)表格要顯示的行數(shù)和列數(shù):

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

3、表格顯示的內(nèi)容:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

4、表格界面:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

七、設(shè)置表格事件

給表格添加數(shù)據(jù)綁定事件:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

設(shè)置了事件以后,雙擊事件即可進(jìn)入代碼編輯界面,綁定事件的代碼如下:

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è)置數(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();
      }
    }
  }
}

八、頁腳顯示打印時(shí)間:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

九、保存報(bào)表格式

設(shè)計(jì)完報(bào)表格式以后,一定要記得保存:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

十、效果

因?yàn)榻缑嫣?,所以分了兩個(gè)截圖顯示:

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

.NET中如何使用FastReport實(shí)現(xiàn)打印功能

讀到這里,這篇“.NET中如何使用FastReport實(shí)現(xiàn)打印功能”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI