C#.NET自定义报表数据打印

简介: 原文:C#.NET自定义报表数据打印     这是一个自定义的报表打印,是对PrintPreviewDialog的扩展和封装。PrintPreviewDialog是一个windows的打印预览控件,该类返回的就是一个PrintPreviewDialog对象了,直接显示该控件就可以了。
原文: C#.NET自定义报表数据打印

  

  这是一个自定义的报表打印,是对PrintPreviewDialog的扩展和封装。PrintPreviewDialog是一个windows的打印预览控件,该类返回的就是一个PrintPreviewDialog对象了,直接显示该控件就可以了。

    该报表主要包含两个方面的内容:一个是数据统计分析的饼状图(数据集是用DataTable保存的,,图形统计值分析DataTable的前两列);另一个就是DataTable的数据集了,可以自己定义绘制的格式(当然得自己写代码了)。

    效果预览图如下所示:

    该类的源代码如下:

 

代码
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4 using System.Text;
5 //
6 using System.Drawing;
7 using System.Drawing.Printing;
8 using System.Data;
9 using System.Windows.Forms;
10 // using Microsoft.Office.Interop.Excel;
11 using System.Reflection;
12
13 namespace WinFormTest
14 {
15 /// <summary>
16 /// 数据报表统计
17 /// ryan-2010/9/19
18 /// </summary>
19 public class DataReprot
20 {
21 #region // property
22 // image size
23 int _Width = 600 ;
24 int _Height = 420 ;
25 // pager
26 private int _TopMargin = 50 ;
27 private int _LeftMargin = 60 ;
28 private int _RightMargin = 50 ;
29 private int _BottomMargin = 60 ;
30 private Font _TitleFont = new Font( " 宋体 " , 18 , FontStyle.Bold);
31 private Font _ColumnsHeaderFont = new Font( " 宋体 " , 10 , FontStyle.Bold);
32 private Font _ContentFont = new Font( " 宋体 " , 9 , FontStyle.Regular);
33 SolidBrush brush = new SolidBrush(Color.Black);
34 Pen pen = new Pen( new SolidBrush(Color.Black));
35 int _RowHeight = 30 ;
36 int _CurrentPageIndex;
37 int _PageCount;
38 int _RowsCount;
39 int _CurrentRowsIndex;
40 int _MaxRowsCount = 35 ;
41 Point _CurrentPoint;
42 DataTable _DT;
43 string _Title;
44 string _ImgTitle;
45 string [] _ColumnsHeader;
46 string [] _BottomStr;
47 #endregion
48
49 #region // DataReprot()
50 public DataReprot( string title, string imgTitle, DataTable dataTable, string [] columnsHeader, string [] bottomStr)
51 {
52 _Title = title;
53 _DT = Sort(dataTable);
54 _ImgTitle = imgTitle;
55 _ColumnsHeader = columnsHeader;
56 _RowsCount = dataTable.Rows.Count;
57 _BottomStr = bottomStr;
58 _CurrentPageIndex = 0 ;
59 _CurrentRowsIndex = 0 ;
60 // pagecount
61 if ((dataTable.Rows.Count + 20 ) % _MaxRowsCount == 0 )
62 _PageCount = (dataTable.Rows.Count + 20 ) / _MaxRowsCount;
63 else
64 _PageCount = ((dataTable.Rows.Count + 20 ) / _MaxRowsCount) + 1 ;
65 }
66 #endregion
67
68 #region // 保存为excl
69 public void SaveAsExcl( string fileFullPath)
70 {
71 Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
72 Microsoft.Office.Interop.Excel.Workbook wBook = excel.Workbooks.Add( true );
73 Microsoft.Office.Interop.Excel.Worksheet wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
74 excel.DisplayAlerts = false ;
75 excel.AlertBeforeOverwriting = false ;
76 //
77 excel.ActiveWorkbook.sav
78 excel.Cells[ 1 , 1 ] = " 网上搜索C#实现excel操作的示例太多了,但不知道有多少是经过验证确实 " ;
79
80 //
81 excel.ActiveWorkbook.SaveCopyAs(fileFullPath);
82 excel.Quit();
83 }
84 #endregion
85
86 #region // 对dt排序
87 public DataTable Sort(DataTable dataTable)
88 {
89 string orderName = dataTable.Columns[ 1 ].ColumnName;
90 DataView dv = dataTable.DefaultView;
91 dv.Sort = orderName + " DESC " ;
92 dataTable = dv.ToTable();
93 return dataTable;
94 }
95 #endregion
96
97 #region // 打印报表
98 public PrintPreviewDialog PrintReport()
99 {
100
101 //
102 PrintDocument printDoc = new PrintDocument();
103 printDoc.PrintPage += PrintPage;
104 printDoc.BeginPrint += BeginPrint;
105 PrintPreviewDialog pPreviewDialog = new PrintPreviewDialog();
106 pPreviewDialog.Document = printDoc;
107 pPreviewDialog.ShowIcon = false ;
108 pPreviewDialog.PrintPreviewControl.Zoom = 1.0 ;
109 pPreviewDialog.TopLevel = false ;
110 SetPrintPreviewDialog(pPreviewDialog);
111 return pPreviewDialog;
112 }
113 #endregion
114
115 #region // Bitmap GetPieImage()
116
117 #region // 绘制饼状图
118 /// <summary>
119 /// 绘制饼状图
120 /// </summary>
121 /// <returns></returns>
122 public Bitmap GetPieImage( string title, DataTable dataTable)
123 {
124 Bitmap image = GenerateImage(title);
125 dataTable = DataFormat(dataTable);
126 // 主区域图形
127 Rectangle RMain = new Rectangle( 35 , 70 , 380 , 300 );
128 // 图例信息
129 Rectangle RDes = new Rectangle( 445 , 90 , 10 , 10 );
130 Font f = new Font( " 宋体 " , 10 , FontStyle.Regular);
131
132 Graphics g = Graphics.FromImage(image);
133 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
134 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
135 try
136 {
137 // 分析数据,绘制饼图和图例说明
138 double [] ItemRate = GetItemRate(dataTable);
139 int [] ItemAngle = GetItemAngle(ItemRate);
140 int Angle1 = 0 ;
141 int Angle2 = 0 ;
142 int len = ItemRate.Length;
143 Color c = new Color();
144 // 3D
145 g.DrawPie( new Pen(Color.Black), RMain, 0F, 360F);
146 g.DrawPie( new Pen(Color.Black), new Rectangle(RMain.X, RMain.Y + 10 , RMain.Width, RMain.Height), 0F, 360F);
147 g.FillPie( new SolidBrush(Color.Black), new Rectangle(RMain.X, RMain.Y + 10 , RMain.Width, RMain.Height), 0F, 360F);
148 // 绘制
149 for ( int i = 0 ; i < len; i ++ )
150 {
151 Angle2 = ItemAngle[i];
152 // if (c != GetRandomColor(i))
153 c = GetRandomColor(i);
154
155 SolidBrush brush = new SolidBrush(c);
156 string DesStr = dataTable.Rows[i][ 0 ].ToString() + " ( " + (ItemRate[i] * 100 ).ToString( " .00 " ) + " % " + " ) " ;
157 //
158 DrawPie(image, RMain, c, Angle1, Angle2);
159 Angle1 += Angle2;
160 DrawDes(image, RDes, c, DesStr, f, i);
161 }
162
163 return image;
164 }
165 finally
166 {
167 g.Dispose();
168 }
169 }
170 #endregion
171
172 #region // 绘制图像的基本数据计算方法
173 /// <summary>
174 /// 数据格式化
175 /// </summary>
176 private DataTable DataFormat(DataTable dataTable)
177 {
178 if (dataTable == null )
179 return dataTable;
180 // 把大于等于10的行合并,
181 if (dataTable.Rows.Count <= 10 )
182 return dataTable;
183 // new Table
184 DataTable dataTableNew = dataTable.Copy();
185 dataTableNew.Rows.Clear();
186 for ( int i = 0 ; i < 8 ; i ++ )
187 {
188 DataRow dataRow = dataTableNew.NewRow();
189 dataRow[ 0 ] = dataTable.Rows[i][ 0 ];
190 dataRow[ 1 ] = dataTable.Rows[i][ 1 ];
191 dataTableNew.Rows.Add(dataRow);
192 }
193 DataRow dr = dataTableNew.NewRow();
194 dr[ 0 ] = " 其它 " ;
195 double allValue = 0 ;
196 for ( int i = 9 ; i < dataTable.Rows.Count; i ++ )
197 {
198 allValue += Convert.ToDouble(dataTable.Rows[i][ 1 ]);
199 }
200 dr[ 1 ] = allValue;
201 dataTableNew.Rows.Add(dr);
202 return dataTableNew;
203 }
204 /// <summary>
205 /// 计算数值总和
206 /// </summary>
207 private static double Sum(DataTable dataTable)
208 {
209 double t = 0 ;
210 foreach (DataRow dr in dataTable.Rows)
211 {
212 t += Convert.ToDouble(dr[ 1 ]);
213 }
214 return t;
215 }
216 /// <summary>
217 /// 计算各项比例
218 /// </summary>
219 private static double [] GetItemRate(DataTable dataTable)
220 {
221 double sum = Sum(dataTable);
222 double [] ItemRate = new double [dataTable.Rows.Count];
223 for ( int i = 0 ; i < dataTable.Rows.Count; i ++ )
224 {
225 ItemRate[i] = Convert.ToDouble(dataTable.Rows[i][ 1 ]) / sum;
226 }
227 return ItemRate;
228 }
229 /// <summary>
230 /// 根据比例,计算各项角度值
231 /// </summary>
232 private static int [] GetItemAngle( double [] ItemRate)
233 {
234 int [] ItemAngel = new int [ItemRate.Length];
235 for ( int i = 0 ; i < ItemRate.Length; i ++ )
236 {
237 double t = 360 * ItemRate[i];
238 ItemAngel[i] = Convert.ToInt32(t);
239 }
240 return ItemAngel;
241 }
242 #endregion
243
244 #region // 随即扇形区域颜色,绘制区域框,
245 /// <summary>
246 /// 生成随机颜色
247 /// </summary>
248 /// <returns></returns>
249 private static Color GetRandomColor( int seed)
250 {
251 Random random = new Random(seed);
252 int r = 0 ;
253 int g = 0 ;
254 int b = 0 ;
255 r = random.Next( 0 , 230 );
256 g = random.Next( 0 , 230 );
257 b = random.Next( 0 , 235 );
258 Color randomcolor = Color.FromArgb(r, g, b);
259 return randomcolor;
260 }
261 /// <summary>
262 /// 绘制区域框、阴影
263 /// </summary>
264 private static Bitmap DrawRectangle(Bitmap image, Rectangle rect)
265 {
266 Bitmap Image = image;
267 Graphics g = Graphics.FromImage(Image);
268 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
269 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
270 try
271 {
272 Rectangle rn = new Rectangle(rect.X + 3 , rect.Y + 3 , rect.Width, rect.Height);
273 SolidBrush brush1 = new SolidBrush(Color.FromArgb( 233 , 234 , 249 ));
274 SolidBrush brush2 = new SolidBrush(Color.FromArgb( 221 , 213 , 215 ));
275 //
276 g.FillRectangle(brush2, rn);
277 g.FillRectangle(brush1, rect);
278 return Image;
279 }
280 finally
281 {
282 g.Dispose();
283 }
284 }
285 #endregion
286
287 #region // 绘制图例框、图列信息,绘制扇形
288 /// <summary>
289 /// 绘制图例信息
290 /// </summary>
291 private static Bitmap DrawDes(Bitmap image, Rectangle rect, Color c, string DesStr, Font f, int i)
292 {
293 Bitmap Image = image;
294 Graphics g = Graphics.FromImage(Image);
295 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
296 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
297 try
298 {
299 SolidBrush brush = new SolidBrush(c);
300 Rectangle R = new Rectangle(rect.X, rect.Y + 25 * i, rect.Width, rect.Height);
301 Point p = new Point(rect.X + 12 , rect.Y + 25 * i);
302 // 颜色矩形框
303 g.FillRectangle(brush, R);
304 // 文字说明
305 g.DrawString(DesStr, f, new SolidBrush(Color.Black), p);
306 return Image;
307 }
308 finally
309 {
310 g.Dispose();
311 }
312 }
313 // 绘制扇形
314 private static Bitmap DrawPie(Bitmap image, Rectangle rect, Color c, int Angle1, int Angle2)
315 {
316 Bitmap Image = image;
317 Graphics g = Graphics.FromImage(Image);
318 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
319 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
320 try
321 {
322 SolidBrush brush = new SolidBrush(c);
323 Rectangle R = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
324 g.FillPie(brush, R, Angle1, Angle2);
325 return Image;
326 }
327 finally
328 {
329 g.Dispose();
330 }
331 }
332 #endregion
333
334 #region // 绘制基本图形
335 /// <summary>
336 /// 生成图片,统一设置图片大小、背景色,图片布局,及标题
337 /// </summary>
338 /// <returns> 图片 </returns>
339 private Bitmap GenerateImage( string Title)
340 {
341 Bitmap image = new Bitmap(_Width, _Height);
342 Graphics g = Graphics.FromImage(image);
343 // 标题
344 Point PTitle = new Point( 30 , 20 );
345 Font f1 = new Font( " 黑体 " , 12 , FontStyle.Bold);
346 // 线
347 int len = ( int )g.MeasureString(Title, f1).Width;
348 Point PLine1 = new Point( 20 , 40 );
349 Point PLine2 = new Point( 20 + len + 20 , 40 );
350 Pen pen = new Pen( new SolidBrush(Color.FromArgb( 8 , 34 , 231 )), 1.5f );
351 // 主区域,主区域图形
352 Rectangle RMain1 = new Rectangle( 20 , 55 , 410 , 345 );
353 Rectangle RMain2 = new Rectangle( 25 , 60 , 400 , 335 );
354 // 图例区域
355 Rectangle RDes1 = new Rectangle( 440 , 55 , 150 , 345 );
356 // 图例说明
357 string Des = " 图例说明: " ;
358 Font f2 = new Font( " 黑体 " , 10 , FontStyle.Bold);
359 Point PDes = new Point( 442 , 65 );
360 // 图例信息,后面的x坐标上累加20
361 Rectangle RDes2 = new Rectangle( 445 , 90 , 10 , 10 );
362 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
363 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
364 try
365 {
366 // 设置背景色、绘制边框
367 g.Clear(Color.White);
368 g.DrawRectangle(pen, 1 , 1 , _Width - 2 , _Height - 2 );
369 // 绘制标题、线
370 g.DrawString(Title, f1, new SolidBrush(Color.Black), PTitle);
371 g.DrawLine(pen, PLine1, PLine2);
372
373 // 主区域
374 image = DrawRectangle(image, RMain1);
375 // 图例区域
376 image = DrawRectangle(image, RDes1);
377 // “图例说明”
378 g.DrawString(Des, f2, new SolidBrush(Color.Black), PDes);
379 // return
380 return image;
381 }
382 finally
383 {
384 g.Dispose();
385 }
386
387 }
388 #endregion
389
390 #endregion
391
392
393 #region // 绘制图形、报表
394
395 #region // print Event
396 private void PrintPage( object sender, PrintPageEventArgs e)
397 {
398 _CurrentPageIndex ++ ;
399 _CurrentPoint = new Point(_LeftMargin, _RightMargin);
400 int serialNumWidth = 60 ;
401 int colWidth = (e.PageBounds.Width - _LeftMargin - _RightMargin - serialNumWidth) / _DT.Columns.Count;
402 // 第一页绘制标题,图形
403 if (_CurrentPageIndex == 1 )
404 {
405 DrawTitle(e);
406 DrawImage(e);
407 DrawTableHeader(e, serialNumWidth, colWidth);
408 DrawBottom(e);
409 DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
410 if (_PageCount > 1 )
411 e.HasMorePages = true ;
412
413 }
414 else if (_CurrentPageIndex == _PageCount)
415 {
416 DrawTableHeader(e, serialNumWidth, colWidth);
417 DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
418 DrawBottom(e);
419 e.HasMorePages = false ;
420 e.Cancel = true ;
421 }
422 else
423 {
424 DrawTableHeader(e, serialNumWidth, colWidth);
425 DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
426 DrawBottom(e);
427 e.HasMorePages = true ;
428
429 }
430 }
431 private void BeginPrint( object sender,PrintEventArgs e)
432 {
433 _CurrentPageIndex = 0 ;
434 _CurrentRowsIndex = 0 ;
435 e.Cancel = false ;
436 }
437 #endregion
438
439 #region // 绘制标题
440 private void DrawTitle(PrintPageEventArgs e)
441 {
442 // 标题 居中
443 _CurrentPoint.X = (e.PageBounds.Width) / 2 - ( int )(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2 ;
444 e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
445 _CurrentPoint.Y += ( int )(e.Graphics.MeasureString(_Title, _TitleFont).Height);
446 // 标题下的线
447 int len = ( int )(e.Graphics.MeasureString(_Title, _TitleFont).Width) + 100 ;
448 int start = (e.PageBounds.Width) / 2 - len / 2 ;
449 e.Graphics.DrawLine( new Pen( new SolidBrush(Color.Black)), new Point(start, _CurrentPoint.Y), new Point(start + len, _CurrentPoint.Y));
450 _CurrentPoint.Y += 3 ;
451 e.Graphics.DrawLine( new Pen( new SolidBrush(Color.Black)), new Point(start, _CurrentPoint.Y), new Point(start + len, _CurrentPoint.Y));
452 _CurrentPoint.Y += 50 ;
453 _CurrentPoint.X = _LeftMargin;
454 }
455
456 #endregion
457
458 #region // 绘制统计图
459 private void DrawImage(PrintPageEventArgs e)
460 {
461 // 标题 居中
462 _CurrentPoint.X = (e.PageBounds.Width) / 2 - _Width / 2 ;
463 e.Graphics.DrawImage(GetPieImage(_ImgTitle, _DT), _CurrentPoint);
464 _CurrentPoint.X = _LeftMargin;
465 _CurrentPoint.Y += _Height + 50 ;
466 }
467
468 #endregion
469
470 #region // 绘制页尾
471 private void DrawBottom(PrintPageEventArgs e)
472 {
473 int pageNumWidth = 70 ;
474 int count = _BottomStr.Length;
475 int width = (e.PageBounds.Width - _LeftMargin - _RightMargin - pageNumWidth) / (count + 1 );
476 int y = e.PageBounds.Height - _BottomMargin + 5 ;
477 int x = _LeftMargin;
478 // line
479 e.Graphics.DrawLine( new Pen( new SolidBrush(Color.Black)), x, y, e.PageBounds.Width - _RightMargin, y);
480 y += 5 ;
481 for ( int i = 0 ; i < count; i ++ )
482 {
483 if (i > 0 )
484 x += width;
485 e.Graphics.DrawString(_BottomStr[i], _ContentFont, new SolidBrush(Color.Black), x, y);
486 }
487 x = e.PageBounds.Width - _RightMargin - pageNumWidth;
488 e.Graphics.DrawString( string .Format( " 第{0}页/共{1}页 " ,_CurrentPageIndex,_PageCount), _ContentFont, new SolidBrush(Color.Black), x, y);
489 }
490
491 #endregion
492
493 #region // 绘制表格和序号、数据
494
495 private void DrawTableAndSerialNumAndData(PrintPageEventArgs e, int serialNumWidth, int colWidth)
496 {
497 int useAbleHeight = e.PageBounds.Height - _CurrentPoint.Y - _BottomMargin;
498 int useAbleRowsCount = useAbleHeight / _RowHeight;
499 int rowsCount = 0 ;
500 if (_RowsCount - _CurrentRowsIndex > useAbleRowsCount)
501 rowsCount = useAbleRowsCount;
502 else
503 rowsCount = _RowsCount - _CurrentRowsIndex;
504 Point pp = new Point(_CurrentPoint.X, _CurrentPoint.Y);
505 for ( int i = 0 ;i <= rowsCount;i ++ )
506 {
507 e.Graphics.DrawLine(pen, _LeftMargin, _CurrentPoint.Y + i * _RowHeight,e.PageBounds.Width - _RightMargin, _CurrentPoint.Y + i * _RowHeight);
508 // 绘制数据
509 if (i >= rowsCount)
510 break ;
511 DrawCellString((i + 1 + _CurrentRowsIndex).ToString(), pp, serialNumWidth,_ContentFont, e);
512 pp.X += serialNumWidth;
513 for ( int j = 0 ; j < _DT.Columns.Count; j ++ )
514 {
515 DrawCellString(_DT.Rows[i + _CurrentRowsIndex][j].ToString(), pp, colWidth, _ContentFont, e);
516 pp.X += colWidth;
517 }
518 pp.Y += _RowHeight;
519 pp.X = _CurrentPoint.X;
520
521 }
522 // 绘制竖线
523 Point p = new Point(_CurrentPoint.X,_CurrentPoint.Y);
524 e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
525 p.X += serialNumWidth;
526 e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
527 for ( int i = 1 ; i < _DT.Columns.Count; i ++ )
528 {
529 p.X += colWidth;
530 e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
531 }
532 p.X = e.PageBounds.Width - _RightMargin;
533 e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
534 _CurrentRowsIndex += rowsCount;
535 }
536
537 #endregion
538
539 #region // 填充数据到单元格
540 private void DrawCellString( string str, Point p, int colWidth,Font f, PrintPageEventArgs e)
541 {
542 int strWidth = ( int )e.Graphics.MeasureString(str, f).Width;
543 int strHeight = ( int )e.Graphics.MeasureString(str, f).Height;
544 p.X += (colWidth - strWidth) / 2 ;
545 p.Y += 5 ;
546 p.Y += (_RowHeight - strHeight) / 2 ;
547 e.Graphics.DrawString(str, f, brush, p);
548 }
549 #endregion
550
551 #region // 绘制标题
552 private void DrawTableHeader(PrintPageEventArgs e, int serialNumWidth, int colWidth)
553 {
554 // 画框
555 Point pp = new Point(_CurrentPoint.X, _CurrentPoint.Y);
556 e.Graphics.DrawLine(pen, pp, new Point(e.PageBounds.Width - _RightMargin, pp.Y));
557 pp.Y += _RowHeight;
558 e.Graphics.DrawLine(pen,pp, new Point(e.PageBounds.Width - _RightMargin,pp.Y));
559 pp = new Point(_CurrentPoint.X, _CurrentPoint.Y);
560 e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
561 pp.X += serialNumWidth;
562 e.Graphics.DrawLine(pen,pp, new Point(pp.X, pp.Y + _RowHeight));
563 for ( int i = 1 ; i < _DT.Columns.Count; i ++ )
564 {
565 pp.X += colWidth;
566 e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
567 }
568 pp.X = e.PageBounds.Width - _RightMargin;
569 e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
570 //
571 Point p = new Point(_CurrentPoint.X + 5 , _CurrentPoint.Y);
572 DrawCellString( " 序号 " , p, serialNumWidth,_ColumnsHeaderFont, e);
573 p.X += serialNumWidth;
574 for ( int i = 0 ; i < _DT.Columns.Count; i ++ )
575 {
576 if (i != 0 )
577 p.X += colWidth;
578 DrawCellString(_ColumnsHeader[i], p, colWidth, _ColumnsHeaderFont, e);
579 }
580 _CurrentPoint.X = _LeftMargin;
581 _CurrentPoint.Y += _RowHeight;
582 }
583 #endregion
584
585 #region // 自定义设置打印预览对话框
586 public void SetPrintPreviewDialog(PrintPreviewDialog pPreviewDialog)
587 {
588 System.Reflection.PropertyInfo[] pis = pPreviewDialog.GetType().GetProperties();
589 for ( int i = 0 ; i < pis.Length; i ++ )
590 {
591 switch (pis[i].Name)
592 {
593 case " Dock " :
594 pis[i].SetValue(pPreviewDialog, DockStyle.Fill, null );
595 break ;
596 case " FormBorderStyle " :
597 pis[i].SetValue(pPreviewDialog, FormBorderStyle.None, null );
598 break ;
599 case " WindowState " :
600 pis[i].SetValue(pPreviewDialog, FormWindowState.Normal, null );
601 break ;
602 default : break ;
603 }
604 }
605 #region // 屏蔽默认的打印按钮,添加自定义的打印和保存按钮
606 foreach (Control c in pPreviewDialog.Controls)
607 {
608 if (c is ToolStrip)
609 {
610 ToolStrip ts = (ToolStrip)c;
611 ts.Items[ 0 ].Visible = false ;
612 // print
613 ToolStripButton toolStripBtn_Print = new ToolStripButton();
614 toolStripBtn_Print.Text = " 打印 " ;
615 toolStripBtn_Print.ToolTipText = " 打印当前报表数据 " ;
616 toolStripBtn_Print.Image = Properties.Resources.printer;
617 toolStripBtn_Print.Click +=
618 delegate ( object sender, EventArgs e)
619 {
620 PrintDialog pd = new PrintDialog();
621 pd.Document = pPreviewDialog.Document;
622 pd.UseEXDialog = true ;
623 if (pd.ShowDialog() == DialogResult.OK)
624 pPreviewDialog.Document.Print();
625 };
626 ToolStripButton toolStripBtn_SaveAsExcel = new ToolStripButton();
627 toolStripBtn_SaveAsExcel.Text = " 保存Excel " ;
628 toolStripBtn_SaveAsExcel.ToolTipText = " 导出报表到Excel " ;
629 toolStripBtn_SaveAsExcel.Image = Properties.Resources.save;
630 toolStripBtn_SaveAsExcel.Click +=
631 delegate ( object sender, EventArgs e)
632 {
633 SaveFileDialog f = new SaveFileDialog();
634
635 if (f.ShowDialog() == DialogResult.OK)
636 {
637 SaveAsExcl(f.FileName);
638 }
639 };
640 ToolStripSeparator tss = new ToolStripSeparator();
641 ts.Items.Insert( 0 , toolStripBtn_Print);
642 ts.Items.Insert( 1 , toolStripBtn_SaveAsExcel);
643 ts.Items.Insert( 2 , tss);
644 }
645 }
646 #endregion
647 }
648 #endregion
649
650 #endregion
651 }
652
653 }
654

 

 

  

   使用法如下代码: 

 

代码
private void button2_Click( object sender, EventArgs e)
{
// 测试数据
DataTable dataTable = new DataTable();
dataTable.Columns.Add(
" id " , typeof ( string ));
dataTable.Columns.Add(
" value1 " , typeof ( double ));
dataTable.Columns.Add(
" value2 " , typeof ( double ));
for ( int i = 0 ; i < 45 ; i ++ )
{
dataTable.Rows.Add(
new object [] { " 北京市- " + i.ToString(), 100 * i, 234.345 * i});
}
string [] bottomStr = { " 操作人员:Ryan " , " 打印日期: " + DateTime.Now.ToShortDateString(), " 审核人员: " , " 财务人员: " };
string [] header = { " 城市名称 " , " 预定数量 " , " 平均价格 " };
DataReprot dr
= new DataReprot( " 2010年12月12日-2011年12月12日城市预订分布统计报表 " , " 2010年12月12日-2011年12月12日城市预订分布统计图 " , dataTable, header, bottomStr);
PrintPreviewDialog p
= dr.PrintReport();
this .groupBox2.Controls.Add(p);
p.Show();
groupBox2.Width
+= 1 ;
this .Refresh();
}

 

 

 

目录
相关文章
|
11天前
|
数据可视化 网络协议 C#
C#/.NET/.NET Core优秀项目和框架2024年3月简报
公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(每周至少会推荐两个优秀的项目和框架当然节假日除外),公众号推文中有项目和框架的介绍、功能特点、使用方式以及部分功能截图等(打不开或者打开GitHub很慢的同学可以优先查看公众号推文,文末一定会附带项目和框架源码地址)。注意:排名不分先后,都是十分优秀的开源项目和框架,每周定期更新分享(欢迎关注公众号:追逐时光者,第一时间获取每周精选分享资讯🔔)。
|
30天前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
8 0
|
1月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
62 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
1月前
|
开发框架 .NET C#
C#数据去重的这几种方式,你知道几种?
C#数据去重的这几种方式,你知道几种?
|
1月前
|
开发框架 算法 搜索推荐
C# .NET面试系列九:常见的算法
#### 1. 求质数 ```c# // 判断一个数是否为质数的方法 public static bool IsPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } class Progr
58 1
|
4天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
25天前
|
SQL C# 数据库
C# 读取多条数据记录导出到 Word 标签模板
C# 读取多条数据记录导出到 Word 标签模板
|
25天前
|
安全 数据处理 C#
C# Post数据或文件到指定的服务器进行接收
C# Post数据或文件到指定的服务器进行接收
|
29天前
|
SQL 数据库
使用ADO.NET查询和操作数据
使用ADO.NET查询和操作数据
9 0
|
1月前
|
C#
C#学习相关系列之自定义遍历器
C#学习相关系列之自定义遍历器