使用FastReport报表工具实现信封套打功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

使用FastReport报表工具实现信封套打功能

伍华聪   2019-12-03 我要评论

在较早期的报表套打的时候,我倾向于使用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报表工具实现信封套打功能的软件,这样整合后,软件体验性就更加完美了。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们