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