报表打印(rdlc)

1首先在你的项目中要添加一个报表文件(扩展名为rdlc)

然后通过VS的可视化对其进行控件的添加和布局

如图:

其中的textbox的位置都是以厘米为单位的
对打印来说非常方便

2针对报表添加一系列的参数
如图:

3打印相关的工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WinForms;
using FB.Entity;
using FB.Helper;
using System.Windows.Forms;

namespace FB.PrintHelper
{
    public class PrintUtils
    {
        public static string PrintPaper(ContractTicket ticket, ContractOrder order, ReportViewer reportViewer1)
        {
            bool flag = false;
            ReportParameter[] rps = new ReportParameter[]
            {
            new ReportParameter("UserName"),
            new ReportParameter("CardNum"),
            new ReportParameter("StartPlace"),
            new ReportParameter("EndPlace"),
            new ReportParameter("FlightName"),
            new ReportParameter("FlightId"),

            new ReportParameter("SeatClass"),
            new ReportParameter("FlyDate"),
            new ReportParameter("FlyTime"),
            new ReportParameter("TicketNum"),//客票级别
            new ReportParameter("PaperNum"),//电子客票号
            new ReportParameter("VerifyNum"),//行程单号

            new ReportParameter("TicketPay"),
            new ReportParameter("AirPortTax"),
            new ReportParameter("TotalPay"),
            new ReportParameter("SafePay"), 
            new ReportParameter("CreateDate"),
            new ReportParameter("EndPlace1"),

            new ReportParameter("FlightName1"),
            new ReportParameter("FlightId1"),
            new ReportParameter("SeatClass1"),
            new ReportParameter("FlyDate1"),
            new ReportParameter("FlyTime1"),
            new ReportParameter("TicketNum1"),

            new ReportParameter("ranyou"),
            new ReportParameter("PNR"),
            new ReportParameter("NewWeight")
            
            };
            StringBuilder sb = new StringBuilder();
            foreach (ContractPassenger P in ticket.Passengers)
            {
                foreach (var r in rps)
                {
                    r.Values.Clear();
                }
                //用户名
                rps[0].Values.Add(P.UserName);
                //用户证件号码
                rps[1].Values.Add(P.Z_ID);
                //起飞城市
                rps[2].Values.Add(Utils.SplitStr(ticket.StartPoint, '#', flag) + "  " + Utils.SplitStr(ticket.AirSC, '#', flag));
                //终点城市
                rps[3].Values.Add(Utils.SplitStr(ticket.EndPoint, '#', flag) + "  " + Utils.SplitStr(ticket.AirSE, '#', flag));
                //承运人
                rps[4].Values.Add(Utils.SplitStr(ticket.Flight_ID, '#', flag).Substring(0, 2));
                //航班号
                rps[5].Values.Add(Utils.SplitStr(ticket.Flight_ID, '#', flag).Substring(2, 4));
                //座位等级
                rps[6].Values.Add(Utils.SplitStr(ticket.CabinType, '#', flag).Split('(')[1].Substring(0, 1));
                //起飞日期
                string[] str = Utils.SplitStr(ticket.FlightDate, '#', flag).Split('-');                
                rps[7].Values.Add(str[2]+Utils.mmonth(str[1]));
                //起飞时间
                rps[8].Values.Add(Utils.SplitStr(ticket.FlightTime, '#', flag).Replace(":", ""));
                //客票级别
                rps[9].Values.Add(rps[6].Values[0] + "/" + Utils.SplitStr(ticket.AirSE, '#', flag) + str[0] +"N20");
                //保险费
                if (P.UserBorn == "1")
                {
                    if (ticket.StartPoint.Contains('#'))
                    {
                        rps[15].Values.Add("40.00");
                    }
                    else
                    {
                        rps[15].Values.Add("20.00");
                    }
                }
                else
                {
                    rps[15].Values.Add("xxx");
                }
                //添开日期
                rps[16].Values.Add(DateTime.Now.ToShortDateString());
                if (ticket.StartPoint.Contains('#'))
                {
                    flag = true;
                    //终点城市
                    rps[17].Values.Add(Utils.SplitStr(ticket.EndPoint, '#', flag) + "  " + Utils.SplitStr(ticket.AirSE, '#', flag));
                    //承运人
                    rps[18].Values.Add(Utils.SplitStr(ticket.Flight_ID, '#', flag).Substring(0, 2));
                    //航班号
                    rps[19].Values.Add(Utils.SplitStr(ticket.Flight_ID, '#', flag).Substring(2, 4));
                    //座位等级
                    rps[20].Values.Add(Utils.SplitStr(ticket.CabinType, '#', flag).Split('(')[1].Substring(0, 1));
                    //起飞日期
                    str = Utils.SplitStr(ticket.FlightDate, '#', flag).Split('-');
                    rps[21].Values.Add(str[2] + Utils.mmonth(str[1]));
                    //起飞时间
                    rps[22].Values.Add(Utils.SplitStr(ticket.FlightTime, '#', flag).Replace(":", ""));
                    //客票级别
                    rps[23].Values.Add(rps[20].Values[0] + "/" + Utils.SplitStr(ticket.AirSE, '#', flag) + str[0] + "N20");
                    //行礼重量
                    rps[26].Values.Add("20k");
                }
                string[] arr = P.PnrText.Split('$');
                //单价
                rps[12].Values.Add("CNY  " + arr[0]);
                //机建
                if (!string.IsNullOrEmpty(arr[1]))
                {
                    rps[13].Values.Add("CN   " + arr[1]);
                }
                else
                {
                    rps[13].Values.Add("EXEMPT");
                }
                //燃油
                if (!string.IsNullOrEmpty(arr[2]))
                {
                    rps[24].Values.Add("CN   " + arr[2]);
                }
                else
                {
                    rps[24].Values.Add("EXEMPT");
                }
                //行程单后四位
                if(!string.IsNullOrEmpty(arr[3]))
                {
                    rps[11].Values.Add(arr[3].Substring(arr[3].Length - 4));
                }
                //电子客票号
                rps[10].Values.Add(arr[4]);
                //PNR
                rps[25].Values.Add(P.PNR);
                //合计
                rps[14].Values.Add("CNY  " + P.Price);
                //不能让他打印空数据
                foreach (ReportParameter p in rps)
                {
                    if (p.Values.Count < 1)
                    {
                        p.Values.Add("  ");
                    }
                }                
                reportViewer1.LocalReport.SetParameters(rps);
                reportViewer1.RefreshReport();
                reportViewer1.Refresh();
                StreamPrintDocument printDoc;
                
                try
                {
                    printDoc = new StreamPrintDocument(reportViewer1.LocalReport);
                    printDoc.Print();
                    sb.AppendFormat("{0}的行程单打印成功\n", P.UserName);
                }
                catch
                {
                    sb.AppendFormat("{0}的行程单打印失败\n", P.UserName);
                }
                printDoc = null;
            }
            return sb.ToString();
        }
    }
}

4调用此工具函数的相关代码

                    Microsoft.Reporting.WinForms.ReportViewer reportViewer1 = new Microsoft.Reporting.WinForms.ReportViewer();
                    reportViewer1.LocalReport.ReportEmbeddedResource = "FB.Client.Report1.rdlc";
                    string message = PrintUtils.PrintPaper(ticket, order, reportViewer1);
                    MessageBox.Show(message);

5打印工具类涉及到的组件和驱动函数
用到了蜡人张的RDLC报表系列所说的知识
RDLC报表

此处就直接提供下载地址:https://files.cnblogs.com/liulun/FB.PrintHelper.rar

原文地址:https://www.cnblogs.com/liulun/p/1612477.html