水晶报表教程:发票套打

使用教程 | 作者:besy | 2014-04-02 11:30:01| 阅读 0 有用 (0) 评论 (0) 收藏


概述:本文教你如何实现水晶报表套打发票。

经常有人在论坛里面询问水晶报表套打的问题,比如如何精准打印发票,如何配置报表数据源,如何选择最佳套打方案等等。下面我就来分享一下水晶报表如何套打的一些经验,说得不全面的地方还请见谅。

水晶报表套打一般分为两种情况:

  1. 清单类打印,在已经印刷好的纸张上打印 ;
  2. 单据发票类,单据格式固定,如每张打印5行数据,而且这种格式是已经印刷好的,当然机打发票除外。 

我们以第2种情况为例进行说明,因为第2种会了第1种也自然就明白了。在这里我们使用程序代码+报表中设置背景图片的方式来实现。 

1、纸张尺寸类型很难控制客户端的,那就用A4纸,计算设置较大右边距和下边距,以适合你的单据尺寸;

2、页面设置,精确计算左边距与上边距,放好你需打印的组件与不需打印的组件(Top与Left);

3、窗体上,三个控件:CrystalReport控件CrystalReportViewer控件PrintDialog控件,让CrystalReportViewer控件的ShowPrintButton=false,以免客户从它启动打印;

4、在制作报表的时候,把你不需要打印的部分使用图片代替。

如下图:

水晶报表套打 CrystalReport

使用了图片来代替固定数据。如上图中的“仓储企业名称”等标题。

5、报表打印类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Easipay.Controls.Printer;

using System.Drawing.Printing;

 

namespace Warehouse.Report

{

    public class ReportAllocateBLM : ReportBase, IPrintReport

    {

        // <summary>

        /// 生成报表对象

        /// </summary>

        /// <returns></returns>

        public object Report()

        {

            return new rptAllocateBLMTHD();

        }

     

        /// <summary>

        /// 打印预览, 设置报表数据的方法:

        /// </summary>

        /// <param name="rpt">报表</param>

        /// <param name="reportData">需要打印的数据</param>

        /// <param name="boolBackgroupPic">是否打印背景图片</param>

        public void PrePrint(ref CrystalDecisions.CrystalReports.Engine.ReportDocument rpt, object reportData, bool boolBackgroupPic)

        {

            ProtionOutHeadReport info = GetDataSet(reportData);

 

            AddBarNo(ref rpt);

 

            rpt.SetDataSource(info);

            System.Drawing.Printing.PrintDocument pd = new PrintDocument();

            System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize("ProtionInHeadReport", 827, 1200);

        }

        ///<summary>

        ///生成报表所需要的DataSet

        ///</summary>

        ///<param name="reportData">由界面中传过来的数据</param>

        ///<returns></returns>

        private ProtionOutHeadReport GetDataSet(object reportData)

        {

            ProtionOutHeadReport outBillDs = new ProtionOutHeadReport();

            Easipay.WGQ.Entitys.ProtionOutHead head = reportData as Easipay.WGQ.Entitys.ProtionOutHead;

            if (head != null)

            {

                FillDataTable<Easipay.WGQ.Entitys.ProtionOutHead>(GetHeadList(head), outBillDs.ProtionOutHead);

            }

            if (head.ProtionOutODetail != null)

            {

                FillDataTable<Easipay.WGQ.Entitys.ProtionOutODetail>(GetDetailList(head), outBillDs.ProtionOutODetail);

            }

            if (head.ProtionOutMDetail != null)

            {

                FillDataTable<Easipay.WGQ.Entitys.ProtionOutMDetail>(GetMDetailList(head), outBillDs.ProtionOutMDetail);

            }

            return outBillDs;

        }

        /// <summary>

        /// 获取报表头

        /// </summary>

        /// <param name="head">进区凭单表头信息</param>

        /// <returns></returns>

        private List<Easipay.WGQ.Entitys.ProtionOutHead> GetHeadList(Easipay.WGQ.Entitys.ProtionOutHead head)

        {

            List<Easipay.WGQ.Entitys.ProtionOutHead> list = new List<Easipay.WGQ.Entitys.ProtionOutHead>();

            list.Add(head);

            return list;

        }

        /// <summary>

        /// 获取报表体

        /// </summary>

        /// <param name="head">进区凭单带表体的表头信息类</param>

        /// <returns></returns>

        private List<Easipay.WGQ.Entitys.ProtionOutODetail> GetDetailList(Easipay.WGQ.Entitys.ProtionOutHead head)

        {

            List<Easipay.WGQ.Entitys.ProtionOutODetail> list = new List<Easipay.WGQ.Entitys.ProtionOutODetail>();

            foreach (Easipay.WGQ.Entitys.ProtionOutODetail item in head.ProtionOutODetail)

            {

                list.Add(item);

            }

 

            return list;

        }

        /// <summary>

        /// 获取报表体

        /// </summary>

        /// <param name="head">进区凭单带表体的表头信息类</param>

        /// <returns></returns>

        private List<Easipay.WGQ.Entitys.ProtionOutMDetail> GetMDetailList(Easipay.WGQ.Entitys.ProtionOutHead head)

        {

            string pid = string.Empty;

            List<Easipay.WGQ.Entitys.ProtionOutMDetail> list = new List<Easipay.WGQ.Entitys.ProtionOutMDetail>();

            foreach (Easipay.WGQ.Entitys.ProtionOutMDetail item in head.ProtionOutMDetail)

            {                

                pid = item.pid;

             if (pid=="2")

             {

                 list.Add(item);

             }

                

            }

//每页要打印八条记录,如果不足8条,则补空行

            while (list.Count < 8)

            {

 

                Easipay.WGQ.Entitys.ProtionOutMDetail detail = new Easipay.WGQ.Entitys.ProtionOutMDetail();

                detail.pid = pid;

                System.Threading.Thread.Sleep(1);

          

                detail.id = (DateTime.UtcNow.Ticks % 100000000000).ToString();               

                list.Add(detail);

            }

            return list;

        }

        string m_PrintTypeBarName;

        /// <summary>

        /// 报表打印类型

        /// </summary>

        public string PrintTypeBarName

        {

            get

            {

                if (string.IsNullOrEmpty(m_PrintTypeBarName))

                {

                    return "分拨货物提货单";

                }

                return m_PrintTypeBarName;

                

            }

            set

            {

                m_PrintTypeBarName = value;

            }

        }

    /// <summary>

        /// 按需要隐藏报表上的某些控件,这里隐藏的都是图片控件,其他的可以自行添加:

        /// </summary>

 

        public void HidePicture(ref CrystalDecisions.CrystalReports.Engine.ReportDocument rpt)

        {

            try

            {

                foreach (CrystalDecisions.CrystalReports.Engine.Section section in rpt.ReportDefinition.Sections)

                {

                    foreach (CrystalDecisions.CrystalReports.Engine.ReportObject rptObject in section.ReportObjects)

                    {

                        if (rptObject is CrystalDecisions.CrystalReports.Engine.PictureObject)

                        {

                            //图片背景

                            (rptObject as CrystalDecisions.CrystalReports.Engine.PictureObject).ObjectFormat.EnableSuppress = true;

                        }

                    }

                }

 

            }

            catch

            {

            }

        }

 

    }

}

6、打印方法:

/// <summary>

/// 打印报表

/// </summary>

/// <param name="strPrintName">打印机名称</param>

/// <param name="shInt">打印份数</param>

/// <returns>true成功,false失败</returns>

private bool blnPrint(string strPrintName,string shInt)

{

bool blnPrintScuss=false;

int intCopies=0;

try

{

intCopies=int.Parse(shInt);

}

catch

{

intCopies=1;

}

try

{

                if (ipr == null)

                {                  

                    MessageBox.Show("打印报表出错");

                    blnPrintScuss = false;

                    return blnPrintScuss;

                }

// m_BillNewRpt.PrintOptions.PaperSize=CrystalDecisions.Shared.PaperSize.PaperA4;

                ipr.HidePicture(ref m_BillNewRpt);

m_BillNewRpt.PrintOptions.PrinterName = strPrintName;

m_BillNewRpt.PrintToPrinter(intCopies,false,1,m_intMaxPage);

blnPrintScuss=true;

}

catch (Exception ex)

{

blnPrintScuss=false;

MessageBox.Show("打印报表出错:"+ex.Message);

}

return blnPrintScuss;

}

7、套打,只打印数据的效果。如下图:

水晶报表套打 CrystalReport

8、非套打,全部打印的效果。如下图:

水晶报表套打 CrystalReport

转自:http://www.cnblogs.com/chillsrc/p/3577290.html


慧都控件|提供软件技术整体解决方案

云集全球三千余款优秀控件、软件产品,提供行业领先的咨询、培训与开发服务
企业QQ:800018081|电话:023-66090381

用户评论: 您的宝贵经验,能为更多人带来帮助,登录后才能评论。
评论加载中...



    联系我们


    官方微信
    官方微博

    慧都旗下网站

    友情链接

    慧都科技有限公司 版权所有 Copyright 2003-2016 渝ICP备12000582号 | 京公网安备 11010102002019号
    100%正版软件