其实使用GDI 画报表也比较简单,只要设定好坐标就行了
下面给个例子看看
代码语言:javascript复制 1 private void button9_Click(object sender, EventArgs e)
2 {
3 printDialog1.ShowDialog(); //设置打印文档
4 printPreviewDialog1.Document = this.printDocument1;
5 printPreviewDialog1.PrintPreviewControl.Zoom = 1;
6 printPreviewDialog1.ClientSize = new Size(800, 800);
7 printPreviewDialog1.ShowDialog();
8 }
9
10 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
11 {
12 //第一条竖线开始30,100
13 //第2条竖线坐标开始130,100
14 //第三条竖线开始230,linebegin,结束230,lineend
15 //第四条竖线开始340,linebegin,结束340,lineend
16 //第五条竖线开始460,linebegin,结束460,lineend
17 //第六条竖线开始670,linebegin,结束670,lineend
18 //最后一条竖线开始790,100
19 int y = 50;
20 int linebegin = 0;
21 int lineend = 0;
22 int end = 0;
23 Font font = new Font("宋体", 10, FontStyle.Regular);
24 Pen title_pen = new Pen(Color.Black, 1);
25 e.Graphics.DrawString("专家抽取列表", new Font("宋体", 20, FontStyle.Bold), Brushes.Black, 270, y);
26 y = 80;
27 e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第一条横线
28 e.Graphics.DrawString("项目名称", font, Brushes.Black, (20 (90 - GetWidth("项目名称", font)) / 2 - 5), y 10);
29 e.Graphics.DrawString(this.txtname.Text.Trim(), font, Brushes.Black, (110 (570 - GetWidth(this.txtname.Text.Trim(), font)) / 2 - 5), y 10);
30 y = 40;
31 e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第二条横线
32 e.Graphics.DrawString("招标单位", font, Brushes.Black, (20 (90 - GetWidth("招标单位", font)) / 2 - 5), y 10);
33 e.Graphics.DrawString(this.txtunit.Text.Trim(), font, Brushes.Black, (110 (570 - GetWidth(this.txtunit.Text.Trim(), font)) / 2 - 5), y 10);
34 y = 40;
35 e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第三条横线
36 e.Graphics.DrawString("抽取时间", font, Brushes.Black, (20 (90 - GetWidth("抽取时间", font)) / 2 - 5), y 10);
37 e.Graphics.DrawString(this.txttime.Text.Trim(), font, Brushes.Black, (110 (570 - GetWidth(this.txttime.Text.Trim(), font)) / 2 - 5), y 10);
38 y = 40;
39 e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第四条横线
40
41 linebegin = y;
42 //开始画列名
43 e.Graphics.DrawString("专业", font, Brushes.Black, (20 (90 - GetWidth("专业", font)) / 2 - 5), y 10);
44 e.Graphics.DrawString("姓名", font, Brushes.Black, (110 (70 - GetWidth("姓名", font)) / 2 - 5), y 10);
45 e.Graphics.DrawString("固定电话", font, Brushes.Black, (180 (90 - GetWidth("固定电话", font)) / 2 - 5), y 10);
46 e.Graphics.DrawString("手机", font, Brushes.Black, (270 (110 - GetWidth("手机", font)) / 2 - 5), y 10);
47 e.Graphics.DrawString("工作单位", font, Brushes.Black, (380 (200 - GetWidth("工作单位", font)) / 2 - 5), y 10);
48 e.Graphics.DrawString("职称", font, Brushes.Black, (580 (110 - GetWidth("职称", font)) / 2 - 5), y 10);
49 y = 40;
50 e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第五条横线
51 //开始画table数据
52 foreach (DataGridViewRow dr in this.dataGridView2.Rows)
53 {
54 e.Graphics.DrawString(dr.Cells[1].Value.ToString(), font, Brushes.Black, (20 (90 - GetWidth(dr.Cells[1].Value.ToString(), font)) / 2 - 5), y 10);
55 e.Graphics.DrawString(dr.Cells[2].Value.ToString(), font, Brushes.Black, (110 (70 - GetWidth(dr.Cells[2].Value.ToString(), font)) / 2 - 5), y 10);
56 int phone = GetWidth(dr.Cells[3].Value.ToString(), font);
57 if (phone <= 90)
58 {
59 e.Graphics.DrawString(dr.Cells[3].Value.ToString(), font, Brushes.Black, (180 (90 - phone) / 2 - 5), y 10);
60 }
61 else
62 {
63 string _phone = dr.Cells[3].Value.ToString();
64 _phone = _phone.Substring(0, 9) "…";
65 phone = GetWidth(_phone, font);
66 e.Graphics.DrawString(_phone, font, Brushes.Black, (180 (90 - phone) / 2 - 5), y 10);
67 }
68 int mobile = GetWidth(dr.Cells[4].Value.ToString(), font);
69 if (mobile <= 100)
70 {
71 e.Graphics.DrawString(dr.Cells[4].Value.ToString(), font, Brushes.Black, (270 (110 - mobile) / 2 - 5), y 10);
72 }
73 else
74 {
75 string _mobile = dr.Cells[4].Value.ToString();
76 _mobile = _mobile.Substring(0, 11) "…";
77 phone = GetWidth(_mobile, font);
78 e.Graphics.DrawString(_mobile, font, Brushes.Black, (270 (110 - phone) / 2 - 5), y 10);
79 }
80 e.Graphics.DrawString(dr.Cells[5].Value.ToString(), font, Brushes.Black, (380 (200 - GetWidth(dr.Cells[5].Value.ToString(), font)) / 2 - 5), y 10);
81 e.Graphics.DrawString(dr.Cells[6].Value.ToString(), font, Brushes.Black, (580 (110 - GetWidth(dr.Cells[6].Value.ToString(), font)) / 2 - 5), y 10);
82 y = 40;
83 e.Graphics.DrawLine(title_pen, 20, y, 690, y);
84 }
85 lineend = y;
86 e.Graphics.DrawString("抽取人签字", font, Brushes.Black, (20 (90 - GetWidth("抽取人签字", font)) / 2 - 5), y 10);
87 y = 40;
88 e.Graphics.DrawLine(title_pen, 20, y, 690, y);
89 e.Graphics.DrawString("监督人签字", font, Brushes.Black, (20 (90 - GetWidth("监督人签字", font)) / 2 - 5), y 10);
90 y = 40;
91 e.Graphics.DrawLine(title_pen, 20, y, 690, y);
92 end = y;
93
94 //开始画竖线
95 //e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
96 //e.Graphics.DrawLine(title_pen, 130, 130, 130, y);
97 //e.Graphics.DrawLine(title_pen, 210, linebegin, 210, lineend);
98 //e.Graphics.DrawLine(title_pen, 320, linebegin, 320, lineend);
99 //e.Graphics.DrawLine(title_pen, 440, linebegin, 440, lineend);
100 //e.Graphics.DrawLine(title_pen, 670, linebegin, 670, lineend);
101 //e.Graphics.DrawLine(title_pen, 800, 130, 800, y);
102 e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
103 e.Graphics.DrawLine(title_pen, 110, 130, 110, y);
104 e.Graphics.DrawLine(title_pen, 180, linebegin, 180, lineend);
105 e.Graphics.DrawLine(title_pen, 270, linebegin, 270, lineend);
106 e.Graphics.DrawLine(title_pen, 380, linebegin, 380, lineend);
107 e.Graphics.DrawLine(title_pen, 580, linebegin, 580, lineend);
108 e.Graphics.DrawLine(title_pen, 690, 130, 690, y);
109 }
110
111 private int GetWidth(string str, Font myf)
112 {
113 Graphics g = Graphics.FromHwnd(this.Handle);
114
115 StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
116
117 SizeF size = g.MeasureString(str, myf, 1000, sf);
118
119 return Convert.ToInt32(size.Width);
120
121 }
效果