1. 程式人生 > >使用FastReport報表工具實現信封套打功能

使用FastReport報表工具實現信封套打功能

在較早期的報表套打的時候,我傾向於使用LODOP的ActiveX進行報表的列印或者套打,BS效果還是很不錯的。之前利用它在Winform程式裡面實現信封套打功能,詳細參考《基於信封套打以及批量列印的實現過程》,雖然功能能夠完美實現,不過由於還需要附帶一個不是百分百整合一起的外掛,還是有點另類的,雖然只是第一次使用的時候,需要安裝一次外掛即可。本篇隨筆介紹一下如何舊瓶裝新酒,使用FastReport報表工具來實現信封的套打及批量列印的功能。

1、信封套打及批量列印功能的回顧

首先我們要有一些特製的信封或者普通訊封,這樣才能基於這個基礎上進行套打,把郵政編碼、地址和聯絡人等資訊列印上去。

然後你需要有一個列印裝置,我這裡採用了一個佳能的噴墨印表機(當然其他的也沒問題)。

其次我們開發一個工具來快速實現資料的匯入和批量列印,如下介面所示。

最後當然能夠滿足要求的列印大量的信封出來,減少我們人工干預的麻煩了。

 

2、使用FastReport報表工具來實現信封的套打及批量列印

首先我們模仿上面的工具介面來做一個新的Winform程式,這次使用DevExpress介面來做,得到介面如下所示。

 

功能和前面軟體的基本一樣,只是介面有所變化差異而已。

現在我們來聊聊如何FastReport報表工具來實現套打的處理,這樣我們就可以使用它來進行信封的列印了。

首先,和前面隨筆《使用FastReport報表工具生成報表PDF文件》/《使用FastReport報表工具生成標籤列印文件》等文章介紹的一樣,我們套打一樣需要準備好一個報表模板,然後基於這個基礎上進行套打處理。

套打的原理,就是預設一個圖片和報表的大小,以及放置一些需要列印的內容,預設的圖片一般不需要打印出來,這樣其他內容列印在特定的紙張上就實現了證書、信封、發票、快遞單等的列印了。

而使用FastReport報表工具,我們可以對報表模板裡面的元素的位置、大小、樣式等進行一定的調整,以適應我們具體的報表需要,基於這個模式我們先設計一個FastReport報表模板,如下所示。

 

以上模板的設定,主要就是注意定義好幾個引數,並將引數和具體的展示控制元件進行繫結,並加入一個圖片作為不列印的元素即可。

報表在執行時刻可以進行模板的調整,如下是報表的【列印設計】介面。

 

我們可以利用FastReport提供的報表設計工具進行元素位置、大小、樣式等方面的調整。這樣就可以給客戶很大的靈活性進行處理。

報表列印的操作如下程式碼所示。

        /// <summary>
        /// 報表列印測試
        /// </summary>
        private void btnPrintTest_Click(object sender, EventArgs e)
        {
            if(this.txtAddress.Text.Length == 0)
            {
                MessageDxUtil.ShowTips("請輸入地址");
                this.txtAddress.Focus();
                return;
            }
            else if (this.txtReceiver.Text.Length == 0)
            {
                MessageDxUtil.ShowTips("請輸入收件人");
                this.txtReceiver.Focus();
                return;
            }

            FrmReportPreview dlg = new FrmReportPreview();
            var report = dlg.Report;

            //載入報表
            var reportFile = Path.Combine(baseDir, "Report/信封報表.frx");
            report.Load(reportFile);

            //繫結資料來源
            //定義引數和資料格式
            var dict = new Dictionary<string, object>();

            var zipCode = txtPostCode.Text.Trim().PadRight(6, ' ').ToCharArray();
            dict.Add("C1", zipCode[0]);
            dict.Add("C2", zipCode[1]);
            dict.Add("C3", zipCode[2]);
            dict.Add("C4", zipCode[3]);
            dict.Add("C5", zipCode[4]);
            dict.Add("C6", zipCode[5]);
            dict.Add("Address", this.txtAddress.Text.Trim());

            var Recipient = this.txtReceiver.Text.Trim();
            if(!Recipient.EndsWith("收"))
            {
                Recipient += "收";
            }
            dict.Add("Recipient", Recipient);

            //重新整理資料來源
            foreach (string key in dict.Keys)
            {
                report.SetParameterValue(key, dict[key]);
            }

            dlg.ShowDialog();
        }

以上列印處理的時候,會呼叫列印預覽介面展示資料,如下介面所示。

 

 報表列印設計處理,和列印測試差不多,也需要繫結資料,方便預覽,程式碼如下所示。

        /// <summary>
        /// 報表列印設計
        /// </summary>
        private void btnPrintDesign_Click(object sender, EventArgs e)
        {
            FrmReportDesign dlg = new FrmReportDesign();
            var report = dlg.Report;

            //載入報表檔案
            var reportFile = Path.Combine(baseDir, "Report/信封報表.frx");
            report.Load(reportFile);

            //繫結資料來源
            //定義引數和資料格式
            var dict = new Dictionary<string, object>();

            var zipCode = txtPostCode.Text.Trim().PadRight(6, ' ').ToCharArray();
            dict.Add("C1", zipCode[0]);
            dict.Add("C2", zipCode[1]);
            dict.Add("C3", zipCode[2]);
            dict.Add("C4", zipCode[3]);
            dict.Add("C5", zipCode[4]);
            dict.Add("C6", zipCode[5]);
            dict.Add("Address", this.txtAddress.Text.Trim());

            var Recipient = this.txtReceiver.Text.Trim();
            if (!Recipient.EndsWith("收"))
            {
                Recipient += "收";
            }
            dict.Add("Recipient", Recipient);

            //重新整理資料來源
            foreach (string key in dict.Keys)
            {
                report.SetParameterValue(key, dict[key]);
            }

            dlg.ShowDialog();
        }

信封列印,我們不需要一個個確認列印對話方塊,指定那個PrintDialog為False即可。信封的批量列印程式碼如下所示。

        /// <summary>
        /// 信封批量列印操作
        /// </summary>
        private void btnBatchPrint_Click(object sender, EventArgs e)
        {
            if(dtImport == null || dtImport.Rows.Count == 0)
            {
                MessageDxUtil.ShowTips("沒有列印資料");
                return;
            }

            FastReport.Report report = new FastReport.Report();
            FastReport.Utils.Res.LocaleFolder = Path.Combine(baseDir, "L18N");
            var file = FastReport.Utils.Res.LocaleFolder + @"Chinese (Simplified).frl";
            FastReport.Utils.Res.LoadLocale(file);

            //載入報表
            var reportFile = Path.Combine(baseDir, "Report/信封報表.frx");
            report.Load(reportFile);

            SplashScreenHelper.Show(typeof(FrmWaitForm));
            SplashScreenHelper.SetCaption("正在列印...");
            int total = dtImport.Rows.Count;
            int i = 0;
            foreach(DataRow dr in dtImport.Rows)
            {
                SplashScreenHelper.SetDescription(string.Format("正在列印...任務 {0} / {1}", i++, total));

                //繫結資料來源
                //定義引數和資料格式
                var dict = new Dictionary<string, object>();

                var zipCode = dr["郵編"].ToString().Trim().PadRight(6, ' ').ToCharArray();
                dict.Add("C1", zipCode[0]);
                dict.Add("C2", zipCode[1]);
                dict.Add("C3", zipCode[2]);
                dict.Add("C4", zipCode[3]);
                dict.Add("C5", zipCode[4]);
                dict.Add("C6", zipCode[5]);
                dict.Add("Address", dr["地址"].ToString().Trim());

                var Recipient = dr["收件人"].ToString().Trim();
                if (!Recipient.EndsWith("收"))
                {
                    Recipient += "收";
                }
                dict.Add("Recipient", Recipient);

                //重新整理資料來源
                foreach (string key in dict.Keys)
                {
                    report.SetParameterValue(key, dict[key]);
                }

                report.PrintSettings.ShowDialog = false;
                report.Print();

                Thread.Sleep(100);
            }
            SplashScreenHelper.SetCaption("列印完成!");
            SplashScreenHelper.SetDescription(string.Format("完成全部列印,共列印【{0}】份!", total));
            Thread.Sleep(500);
            SplashScreenHelper.Close();
        }

我們使用該批量模式測試列印幾個信封,效果如下所示。

 

如果匯入資料很多,那麼一樣和前面的軟體列印效果一樣,中間不需要人工接入,喝茶等著完成即可。

 

這個就是整合了FastReport報表工具實現信封套打功能的軟體,這樣整合後,軟體體驗性就更加完美了。