TT和LG编程系列之实体数据打印
因为Itt8项目需要,今天下午就试着写了个打印类,功能不是很强,也因为Itt8项目要求不是很大,只需把DataGridView中的数据打印出来就行.
其实网上有一些DataGridView打印类和解决方案,但自己还是写一个属于自己的.把它贴出来,让初学者参考下,可以扩充一些.............
不说多了,用代码说话.
先来看张效果图:
下面我们来看看我们的实体类
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6
7/**//***************************************
8 * 类名:FreightOrder
9 * 描述:托运单实体类
10 * 作者:刘江
11 * 创建日期:208-8-30
12 * *************************************/
13namespace PrintDemo
14{
15 /**//// <summary>
16 /// 托运单实体类
17 /// </summary>
18 [Serializable]
19 public class FreightOrder
20 {
21 --- Propertity ---#region --- Propertity ---
22 /**//// <summary>
23 /// 托运单号
24 /// </summary>
25 public string FreightOrderId
26 {
27 get;
28 set;
29 }
30
31 /**//// <summary>
32 /// 代付单号
33 /// </summary>
34 public string PayOrderId
35 {
36 get;
37 set;
38 }
39
40 /**//// <summary>
41 /// 托运单备注
42 /// </summary>
43 public string Description
44 {
45 get;
46 set;
47 }
48
49 /**//// <summary>
50 /// 贷物名称
51 /// </summary>
52 public string FreightName
53 {
54 get;
55 set;
56 }
57
58 /**//// <summary>
59 /// 货物代号
60 /// </summary>
61 public string FreightCode
62 {
63 get;
64 set;
65 }
66
67 /**//// <summary>
68 /// 体积
69 /// </summary>
70 public double? Volume
71 {
72 get;
73 set;
74 }
75
76 /**//// <summary>
77 /// 体量
78 /// </summary>
79 public double? Weight
80 {
81 get;
82 set;
83 }
84
85 /**//// <summary>
86 /// 其他
87 /// </summary>
88 public double? OtherExpense
89 {
90 get;
91 set;
92 }
93
94 /**//// <summary>
95 /// 保险
96 /// </summary>
97 public double? Coverage
98 {
99 get;
100 set;
101 }
102 #endregion
103
104 }
105}
106
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6
7/**//***************************************
8 * 类名:FreightOrder
9 * 描述:托运单实体类
10 * 作者:刘江
11 * 创建日期:208-8-30
12 * *************************************/
13namespace PrintDemo
14{
15 /**//// <summary>
16 /// 托运单实体类
17 /// </summary>
18 [Serializable]
19 public class FreightOrder
20 {
21 --- Propertity ---#region --- Propertity ---
22 /**//// <summary>
23 /// 托运单号
24 /// </summary>
25 public string FreightOrderId
26 {
27 get;
28 set;
29 }
30
31 /**//// <summary>
32 /// 代付单号
33 /// </summary>
34 public string PayOrderId
35 {
36 get;
37 set;
38 }
39
40 /**//// <summary>
41 /// 托运单备注
42 /// </summary>
43 public string Description
44 {
45 get;
46 set;
47 }
48
49 /**//// <summary>
50 /// 贷物名称
51 /// </summary>
52 public string FreightName
53 {
54 get;
55 set;
56 }
57
58 /**//// <summary>
59 /// 货物代号
60 /// </summary>
61 public string FreightCode
62 {
63 get;
64 set;
65 }
66
67 /**//// <summary>
68 /// 体积
69 /// </summary>
70 public double? Volume
71 {
72 get;
73 set;
74 }
75
76 /**//// <summary>
77 /// 体量
78 /// </summary>
79 public double? Weight
80 {
81 get;
82 set;
83 }
84
85 /**//// <summary>
86 /// 其他
87 /// </summary>
88 public double? OtherExpense
89 {
90 get;
91 set;
92 }
93
94 /**//// <summary>
95 /// 保险
96 /// </summary>
97 public double? Coverage
98 {
99 get;
100 set;
101 }
102 #endregion
103
104 }
105}
106
下面打印逻辑:
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6
7using System.Drawing;
8using System.Drawing.Printing;
9using System.Collections;
10using System.Reflection;
11
12/**//***************************************
13 * 类名:EntityReport
14 * 描述:实体数据打印
15 * 作者:刘江
16 * 创建日期:208-8-30
17 * 版权:********************************/
18
19namespace PrintDemo
20{
21 public class EntityReport
22 {
23 //要打印的文档
24 private PrintDocument printDocument = null;
25
26 //起始位置X抽
27 private int x = 50;
28
29 //起始位置Y抽
30 private int y = 50;
31
32 //要打印的列宽
33 private int colWidth = 90;
34
35 //要打印的行高
36 private int rowHeight = 22;
37
38 //总的列宽
39 private int totalWidth = 0;
40
41 //标题
42 private string title = "itt8物流管理平台";
43
44 //所有的列名
45 private PropertyInfo[] propertyList;
46
47 //字符串格式
48 private StringFormat format = null;
49
50 //笔
51 private Pen pen = new Pen(Brushes.Black);
52
53 //字体
54 private Font font = new Font("微软雅黑", 9.0F);
55
56 //页码
57 private int pageNo = 1;
58
59 public List<object> ListSource
60 {
61 get;
62 set;
63 }
64
65 public EntityReport(List<object> listSource, int x, int y, string title)
66 {
67 this.x = x;
68 this.y = y;
69 this.ListSource = listSource;
70 this.title = title;
71
72 printDocument = new PrintDocument();
73 printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
74 printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
75 }
76
77 /**//// <summary>
78 /// 打印事件
79 /// </summary>
80 /// <param name="sender"></param>
81 /// <param name="e"></param>
82 private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
83 {
84 Graphics g = e.Graphics;
85
86 int tempTop = y;
87 int tempLeft = x;
88 int currentRow = 0;
89
90 //设置是否为横向纸张
91 if (totalWidth > e.PageSettings.Bounds.Width - x * 2)
92 {
93 e.PageSettings.Landscape = false;
94 }
95 try
96 {
97 if (pageNo == 1)
98 {
99
100 //打印出一条线
101 g.DrawLine(pen, x, y, e.PageSettings.Bounds.Width - x * 2, y);
102 tempTop += 10;
103 Font fontHead = new Font("微软雅黑", 14.0F);
104 g.DrawString(title, fontHead, Brushes.Black, x + 200, tempTop);
105 tempTop += rowHeight + 10;
106 }
107 DrawHead(g, ref tempTop);
108 while (currentRow < ListSource.Count)
109 {
110 if (tempTop + rowHeight > e.PageSettings.Bounds.Height + y * 2)
111 {
112 //下一页了
113 pageNo++;
114 DrawHead(g, ref tempTop);
115 tempTop = y;
116 }
117 else
118 {
119 //打印数据
120
121 Rectangle rct = new Rectangle(tempLeft, tempTop, colWidth, rowHeight);
122 foreach (PropertyInfo pi in propertyList)
123 {
124 string value = pi.GetValue(ListSource[currentRow], null).ToString();
125 g.DrawRectangle(pen, rct);
126 g.DrawString(value, font, Brushes.Black, rct);
127 tempLeft += colWidth;
128 rct = new Rectangle(tempLeft, tempTop, colWidth, rowHeight);
129 }
130 tempTop += rowHeight;
131 tempLeft = x;
132 currentRow++;
133 }
134 }
135
136 }
137 catch (Exception ex)
138 {
139 throw ex;
140 }
141 finally
142 {
143 e.Graphics.Dispose();
144 }
145 }
146
147 /**//// <summary>
148 /// 打印表头
149 /// </summary>
150 /// <param name="g"></param>
151 /// <param name="tempTop"></param>
152 private void DrawHead(Graphics g, ref int tempTop)
153 {
154 int tempX = x;
155 int tempY = tempTop;
156 if (null == propertyList)
157 {
158 propertyList = ListSource[0].GetType().GetProperties();
159 }
160 Rectangle rct = new Rectangle(tempX, tempTop, colWidth, rowHeight);
161 foreach (PropertyInfo pi in propertyList)
162 {
163 g.DrawRectangle(pen, rct);
164 g.DrawString(pi.Name, font, Brushes.Black, rct);
165 tempX += colWidth;
166 rct = new Rectangle(tempX, tempTop, colWidth, rowHeight);
167 }
168 tempTop += rowHeight;
169 }
170
171 /**//// <summary>
172 /// 开始打印前的工作
173 /// </summary>
174 /// <param name="sender"></param>
175 /// <param name="e"></param>
176 private void printDocument_BeginPrint(object sender, PrintEventArgs e)
177 {
178 //得到所有的列名
179 Type entityType = ListSource[0].GetType();
180 PropertyInfo[] propertyList = entityType.GetProperties();
181
182 //计算总宽度
183 totalWidth = propertyList.Length * colWidth;
184
185 format = new StringFormat();
186 format.Alignment = StringAlignment.Near;
187 format.LineAlignment = StringAlignment.Center;
188 format.Trimming = StringTrimming.EllipsisCharacter;
189 }
190
191 /**//// <summary>
192 /// 打印方法
193 /// </summary>
194 public void Print()
195 {
196 printDocument.Print();
197 }
198 }
199}
200
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6
7using System.Drawing;
8using System.Drawing.Printing;
9using System.Collections;
10using System.Reflection;
11
12/**//***************************************
13 * 类名:EntityReport
14 * 描述:实体数据打印
15 * 作者:刘江
16 * 创建日期:208-8-30
17 * 版权:********************************/
18
19namespace PrintDemo
20{
21 public class EntityReport
22 {
23 //要打印的文档
24 private PrintDocument printDocument = null;
25
26 //起始位置X抽
27 private int x = 50;
28
29 //起始位置Y抽
30 private int y = 50;
31
32 //要打印的列宽
33 private int colWidth = 90;
34
35 //要打印的行高
36 private int rowHeight = 22;
37
38 //总的列宽
39 private int totalWidth = 0;
40
41 //标题
42 private string title = "itt8物流管理平台";
43
44 //所有的列名
45 private PropertyInfo[] propertyList;
46
47 //字符串格式
48 private StringFormat format = null;
49
50 //笔
51 private Pen pen = new Pen(Brushes.Black);
52
53 //字体
54 private Font font = new Font("微软雅黑", 9.0F);
55
56 //页码
57 private int pageNo = 1;
58
59 public List<object> ListSource
60 {
61 get;
62 set;
63 }
64
65 public EntityReport(List<object> listSource, int x, int y, string title)
66 {
67 this.x = x;
68 this.y = y;
69 this.ListSource = listSource;
70 this.title = title;
71
72 printDocument = new PrintDocument();
73 printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
74 printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
75 }
76
77 /**//// <summary>
78 /// 打印事件
79 /// </summary>
80 /// <param name="sender"></param>
81 /// <param name="e"></param>
82 private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
83 {
84 Graphics g = e.Graphics;
85
86 int tempTop = y;
87 int tempLeft = x;
88 int currentRow = 0;
89
90 //设置是否为横向纸张
91 if (totalWidth > e.PageSettings.Bounds.Width - x * 2)
92 {
93 e.PageSettings.Landscape = false;
94 }
95 try
96 {
97 if (pageNo == 1)
98 {
99
100 //打印出一条线
101 g.DrawLine(pen, x, y, e.PageSettings.Bounds.Width - x * 2, y);
102 tempTop += 10;
103 Font fontHead = new Font("微软雅黑", 14.0F);
104 g.DrawString(title, fontHead, Brushes.Black, x + 200, tempTop);
105 tempTop += rowHeight + 10;
106 }
107 DrawHead(g, ref tempTop);
108 while (currentRow < ListSource.Count)
109 {
110 if (tempTop + rowHeight > e.PageSettings.Bounds.Height + y * 2)
111 {
112 //下一页了
113 pageNo++;
114 DrawHead(g, ref tempTop);
115 tempTop = y;
116 }
117 else
118 {
119 //打印数据
120
121 Rectangle rct = new Rectangle(tempLeft, tempTop, colWidth, rowHeight);
122 foreach (PropertyInfo pi in propertyList)
123 {
124 string value = pi.GetValue(ListSource[currentRow], null).ToString();
125 g.DrawRectangle(pen, rct);
126 g.DrawString(value, font, Brushes.Black, rct);
127 tempLeft += colWidth;
128 rct = new Rectangle(tempLeft, tempTop, colWidth, rowHeight);
129 }
130 tempTop += rowHeight;
131 tempLeft = x;
132 currentRow++;
133 }
134 }
135
136 }
137 catch (Exception ex)
138 {
139 throw ex;
140 }
141 finally
142 {
143 e.Graphics.Dispose();
144 }
145 }
146
147 /**//// <summary>
148 /// 打印表头
149 /// </summary>
150 /// <param name="g"></param>
151 /// <param name="tempTop"></param>
152 private void DrawHead(Graphics g, ref int tempTop)
153 {
154 int tempX = x;
155 int tempY = tempTop;
156 if (null == propertyList)
157 {
158 propertyList = ListSource[0].GetType().GetProperties();
159 }
160 Rectangle rct = new Rectangle(tempX, tempTop, colWidth, rowHeight);
161 foreach (PropertyInfo pi in propertyList)
162 {
163 g.DrawRectangle(pen, rct);
164 g.DrawString(pi.Name, font, Brushes.Black, rct);
165 tempX += colWidth;
166 rct = new Rectangle(tempX, tempTop, colWidth, rowHeight);
167 }
168 tempTop += rowHeight;
169 }
170
171 /**//// <summary>
172 /// 开始打印前的工作
173 /// </summary>
174 /// <param name="sender"></param>
175 /// <param name="e"></param>
176 private void printDocument_BeginPrint(object sender, PrintEventArgs e)
177 {
178 //得到所有的列名
179 Type entityType = ListSource[0].GetType();
180 PropertyInfo[] propertyList = entityType.GetProperties();
181
182 //计算总宽度
183 totalWidth = propertyList.Length * colWidth;
184
185 format = new StringFormat();
186 format.Alignment = StringAlignment.Near;
187 format.LineAlignment = StringAlignment.Center;
188 format.Trimming = StringTrimming.EllipsisCharacter;
189 }
190
191 /**//// <summary>
192 /// 打印方法
193 /// </summary>
194 public void Print()
195 {
196 printDocument.Print();
197 }
198 }
199}
200
程序调用测试代码:
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6/**//***************************************
7 * 类名:Program
8 * 描述:打印程序
9 * 作者:刘江
10 * 创建日期:208-8-30
11 * *************************************/
12
13namespace PrintDemo
14{
15 class Program
16 {
17 static void Main(string[] args)
18 {
19 //测试数据
20 List<object> listOrder = new List<object>();
21 for (int i = 0; i < 40; i++)
22 {
23 FreightOrder fo = new FreightOrder();
24
25 fo.Coverage = 1000;
26 fo.Description = "日子不好过";
27 fo.FreightCode = "B40";
28 fo.FreightName = "食物";
29 fo.FreightOrderId = "B000001";
30 fo.FreightCode = "B14";
31 fo.Volume = 100;
32 fo.Weight = 150;
33 fo.PayOrderId = "D1200000";
34 fo.OtherExpense = 150;
35 listOrder.Add(fo);
36 }
37 EntityReport report = new EntityReport(listOrder, 10, 60, "Itt8物流管理平台");
38 report.Print();
39
40 Console.ReadKey();
41 }
42 }
43}
44
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6/**//***************************************
7 * 类名:Program
8 * 描述:打印程序
9 * 作者:刘江
10 * 创建日期:208-8-30
11 * *************************************/
12
13namespace PrintDemo
14{
15 class Program
16 {
17 static void Main(string[] args)
18 {
19 //测试数据
20 List<object> listOrder = new List<object>();
21 for (int i = 0; i < 40; i++)
22 {
23 FreightOrder fo = new FreightOrder();
24
25 fo.Coverage = 1000;
26 fo.Description = "日子不好过";
27 fo.FreightCode = "B40";
28 fo.FreightName = "食物";
29 fo.FreightOrderId = "B000001";
30 fo.FreightCode = "B14";
31 fo.Volume = 100;
32 fo.Weight = 150;
33 fo.PayOrderId = "D1200000";
34 fo.OtherExpense = 150;
35 listOrder.Add(fo);
36 }
37 EntityReport report = new EntityReport(listOrder, 10, 60, "Itt8物流管理平台");
38 report.Print();
39
40 Console.ReadKey();
41 }
42 }
43}
44
结束;功能不足,可以跟据您自己的需要进行扩兖,其实有些细节问题我没有考虑!我这人比较懒,够我用就OK,所以就写了这么多!
源码下载:下载程序
作者:Louja
出处:http://louja.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面给出原文连接,否则保留追究法律责任的权利。