winfor应用程序打印报表清单

最近一周竟然有2位以前的同事问我在winfor应用程序里面打印怎么搞,所以才有了写这篇文章的打算,索性现在没事就写出来  

在窗体上简单的布局设置一下如图

 

定义一个Model 我在里面放了属性之外还从写了ToString方法和返回模型的方法。

代码如下

 1  #region Model
 2             public DateTime CreateDate = DateTime.Today;
 3             public string DocNumber;
 4             public string Title { get; set; }
 5             public string Consignee { get; set; }
 6             public string ProductName { get; set; }
 7             public string ProductLot { get; set; }
 8             public string Specification { get; set; }
 9             public string Factory { get; set; }
10             public string RegNo { get; set; }
11             public string PermitNo { get; set; }
12             public DateTime ExpirationDate { get; set; }
13             public DateTime DeliveryDate { get; set; }
14             public string Unit { get; set; }
15             public int Count { get; set; }
16             public float Price { get; set; }
17             public float Money { get; set; } 
18         #endregion
19         
20 
21         private const char Spliter = '|';//分割txt里面的值
22 
23 
24         public override string ToString()
25         {
26             StringBuilder sb = new StringBuilder();
27             sb.Append(CreateDate);
28             sb.Append(Spliter);
29             sb.Append(DocNumber);
30             sb.Append(Spliter);
31             sb.Append(Title);
32             sb.Append(Spliter);
33             sb.Append(Consignee);
34             sb.Append(Spliter);
35             sb.Append(ProductName);
36             sb.Append(Spliter);
37             sb.Append(ProductLot);
38             sb.Append(Spliter);
39             sb.Append(Specification);
40             sb.Append(Spliter);
41             sb.Append(Factory);
42             sb.Append(Spliter);
43             sb.Append(RegNo);
44             sb.Append(Spliter);
45             sb.Append(PermitNo);
46             sb.Append(Spliter);
47             sb.Append(ExpirationDate);
48             sb.Append(Spliter);
49             sb.Append(DeliveryDate);
50             sb.Append(Spliter);
51             sb.Append(Unit);
52             sb.Append(Spliter);
53             sb.Append(Count);
54             sb.Append(Spliter);
55             sb.Append(Price);
56             sb.Append(Spliter);
57             sb.Append(Money);
58             sb.Append(Spliter);
59             return sb.ToString();
60         }
61 
62         public static OutshipDoc FromString(string s)
63         {
64             try
65             {
66                 int i = 0;
67 
68                 OutshipDoc doc = new OutshipDoc();
69                 string[] pars = s.Split(new char[] { Spliter });
70                 doc.CreateDate = DateTime.Parse(pars[i++]);
71                 doc.DocNumber = pars[i++];
72                 doc.Title = pars[i++];
73                 doc.Consignee = pars[i++];
74                 doc.ProductName = pars[i++];
75                 doc.ProductLot = pars[i++];
76                 doc.Specification = pars[i++];
77                 doc.Factory = pars[i++];
78                 doc.RegNo = pars[i++];
79                 doc.PermitNo = pars[i++];
80                 doc.ExpirationDate = DateTime.Parse(pars[i++]);
81                 doc.DeliveryDate = DateTime.Parse(pars[i++]);
82                 doc.Unit = pars[i++];
83                 doc.Count = int.Parse(pars[i++]);
84                 doc.Price = float.Parse(pars[i++]);
85                 doc.Money = float.Parse(pars[i++]);
86                 return doc;
87             }
88             catch (System.Exception ex)
89             {
90                 return null;
91             }
92         }

Model里面的东西已经准备完了,现在就开始前台的代码了

 OutshipDoc doc;

        internal OutshipDoc Doc
        {
            get { return doc; }
            set { doc = value; }
        }

以上是model,接下来就是定义公用的

PrintDocument
private System.Drawing.Printing.PrintDocument docToPrint =
           new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例

定义好了就用构造函数初始化Model

 public PrintPreview()
        {
            InitializeComponent();
            doc = new OutshipDoc();
            doc.Title = "广州市xx医疗器械有限公司送货清单";
            doc.Consignee = "阳江人民医院";
            doc.ProductName = "银尔通活性银离子抗菌液III型";
            doc.DocNumber = "X2012040201";
            doc.DeliveryDate = DateTime.Parse("2012/4/2");
            doc.Factory = "西安康旺抗菌科技股份有限公司";
            doc.Specification = "1200ml/盒";
            doc.RegNo = "陕食药监械(准)字2008第2640064号";
            doc.PermitNo = "陕食药监械生产许20052286号";
            doc.ProductLot = "20110402";
            doc.ExpirationDate = DateTime.Parse("2014/04/01");
            doc.Unit = "";
            doc.Count = 960;
            doc.Price = 39.00F;
            doc.Money = 37441.18F;

            docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
        }
//画打印格式
        void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int margin = 20;
            int width = 750;
            int height  = e.PageBounds.Height;
            int left = (e.PageBounds.Width - width) / 2;
            int top = 20;
            renderDocument(doc, e.Graphics, left, top, width, height);
            
        }
public static void renderDocument(OutshipDoc d, Graphics g, int left, int top, int width, int height)
        {
            width = 750;

            Brush br = Brushes.Black;

            // 打印抬头
            Font ft;
            StringFormat sf;

            ft = new Font("宋体", 14);
            sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.FormatFlags = StringFormatFlags.NoClip;

            g.DrawString(d.Title, ft, br
                , new RectangleF(new PointF(left, top), new SizeF(width , height ))
                , sf);

            string info1 = "收货单位:" + d.Consignee + "   " + "品名:" + d.ProductName;
            ft.Dispose();
            ft = new Font("宋体", 12);
            g.DrawString(info1, ft, br, new PointF(left, top + 50));

            info1 = "编号:" + d.DocNumber;
            g.DrawString(info1, ft, br, new PointF(left + width - 200, top + 50));

            ft.Dispose();
            ft = new Font("宋体", 10);
            info1 = "生产厂家:" + d.Factory;
            g.DrawString(info1, ft, br, new PointF(left, top + 80));
            info1 = "送货日期:" + d.DeliveryDate.Year + "" + d.DeliveryDate.Month + "" + d.DeliveryDate.Day + "";
            g.DrawString(info1, ft, br, new PointF(left + width - 200, top + 80));

            int x = left, y = top + 100;

            Pen pn = Pens.Black;
            g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
            y += 50;
            g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
            y += 50;
            g.DrawLine(pn, new Point(left, y), new Point(left + 80, y));
            g.DrawLine(pn, new Point(left + 80 + 240, y), new Point(left + width, y));
            y += 50;
            g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
            y += 50;
            g.DrawLine(pn, new Point(left, y), new Point(left + width, y));

            int y1 = top + 100;
            int y2 = top + 250;
            int y3 = top + 300;
            g.DrawLine(pn, new Point(x, y1), new Point(left, y3));
            x += 80;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
            x += 120;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 120;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 80;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 80;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 60;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 60;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
            x += 60;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
            x = left + width;
            g.DrawLine(pn, new Point(x, y1), new Point(x, y3));

            sf.Alignment =  StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;

            ft.Dispose();
            ft = new Font("宋体", 11);

            
            x = left;
            g.DrawString("规格", ft, br, new RectangleF(x, y1, 80, 50), sf);
            x += 80;
            g.DrawString("产品注册证号", ft, br, new RectangleF(x, y1, 120, 50), sf);
            x += 120;
            g.DrawString("医疗器械生产企业许可证号", ft, br, new RectangleF(x, y1, 120, 50), sf);
            x += 120;
            g.DrawString("产品批号", ft, br, new RectangleF(x, y1, 80, 50), sf);
            x += 80;
            g.DrawString("失效日期", ft, br, new RectangleF(x, y1, 80, 50), sf);
            x += 80;
            g.DrawString("单位", ft, br, new RectangleF(x, y1, 60, 50), sf);
            x += 60;
            g.DrawString("数量", ft, br, new RectangleF(x, y1, 60, 50), sf);
            x += 60;
            g.DrawString("单价", ft, br, new RectangleF(x, y1, 60, 50), sf);
            x += 60;
            g.DrawString("金额", ft, br, new RectangleF(x, y1, 100, 50), sf);
            x += 150;

            x = left;
            g.DrawString(d.Specification, ft, br, new RectangleF(x, y1 + 50, 80, 50), sf);
            x += 80;
            g.DrawString(d.RegNo, ft, br, new RectangleF(x, y1 + 50, 120, 80), sf);
            x += 120;
            g.DrawString(d.PermitNo, ft, br, new RectangleF(x, y1 + 50, 120, 80), sf);
            x += 120;
            g.DrawString(d.ProductLot, ft, br, new RectangleF(x, y1 + 50, 80, 50), sf);
            x += 80;
            g.DrawString(d.ExpirationDate.ToString("yyyyMMdd"), ft, br, new RectangleF(x, y1 + 50, 80, 50), sf);
            x += 80;
            g.DrawString(d.Unit, ft, br, new RectangleF(x, y1 + 50, 60, 50), sf);
            x += 60;
            g.DrawString(d.Count.ToString(), ft, br, new RectangleF(x, y1 + 50, 60, 50), sf);
            x += 60;
            g.DrawString(d.Price.ToString("#.00"), ft, br, new RectangleF(x, y1 + 50, 60, 50), sf);
            x += 60;
            g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y1 + 50, 100, 50), sf);
            x += 100;

            x = left;
            sf.Alignment = StringAlignment.Near;
            g.DrawString("合计:", ft, br, new RectangleF(x, y2, 100, 50), sf);
            x += 80;
            g.DrawString("人民币(大写)" + moneyToString(d.Money), ft, br, new RectangleF(x, y2, 500, 50), sf);
            x += 120;
            x += 120;
            x += 80;
            x += 80;
            x += 60;
            x += 60;
            x += 60;
            sf.Alignment = StringAlignment.Center;
            g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y2, 100, 50), sf);
            x += 100;

            x = left;
            sf.Alignment = StringAlignment.Near;
            g.DrawString("收货单位:", ft, br, new RectangleF(x, y3, 400, 50), sf);
            x += 400;
            g.DrawString("送货单位:", ft, br, new RectangleF(x, y3, 400, 50), sf);

 
        }
 //货币大写转换
        private static string moneyToString(double p)
        {
            string[] numbers = new string[] { "", "", "", "", "", "", "", "", "", ""};
            string[] unit = new string[] { "", "", "", "", "", "拾万", "佰万", "仟万" };
            string[] xunit = new string[] { "", "" };

            int x = (int)p;

            string s = "";
            int i = 0;
            for (i = 0; i < 8 && x > 0; i++)
            {
                if (x % 10 != 0)
                    s = numbers[x % 10] + unit[i] + s;
                x /= 10;
            }
            if (s == "")
                s = "";
            if (!s.EndsWith(""))
                s += "";

            double y = p - (int)p;
            if (y != 0.0f)
            {
                y *= 10;
                if ((int)y != 0)
                    s += numbers[(int)y] + "";
                y = y - (int)y;
                y *= 10;
                if ((int)y != 0)
                    s += numbers[(int)y] + "";
            }

            if (s.EndsWith(""))
                s += "";
            return s;
        }

输出的格式浏览就已经写完最好就差真正的打印了

private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            pd.Document = docToPrint;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                docToPrint.Print();   
            }
        }

到此就已经写完了看看效果了,如图下面就是最后的效果

原文地址:https://www.cnblogs.com/jxluowei/p/3217145.html