win打印.doc_第1页
win打印.doc_第2页
win打印.doc_第3页
win打印.doc_第4页
win打印.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Winform 打印报表(从网上看到的代码,感觉不错,上传以做备忘)winform 打印原理.1.先在画布上面画好表格2.然后传入要打印的数据代码如下:using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Drawing.Printing;using System.Drawing;namespace Etaocn.Util public class Printer private DataGridView dataview; private PrintDocument printDoc; /打印有效区域的宽度 int width; int height; int columns; double Rate; bool hasMorePage = false; int currRow = 0; int rowHeight = 20; /打印页数 int PageNumber; /当前打印页的行数 int pageSize = 20; /当前打印的页码 int PageIndex; int AreaHeight; private int PageWidth; /打印纸的宽度 private int PageHeight; /打印纸的高度 private int LeftMargin; /有效打印区距离打印纸的左边大小 private int TopMargin;/有效打印区距离打印纸的上面大小 private int RightMargin;/有效打印区距离打印纸的右边大小 private int BottomMargin;/有效打印区距离打印纸的下边大小 int rows; private string footer; /*/ / 构造函数 / / 要打印的DateGridView / PrintDocument用于获取打印机的设置 public Printer(DataGridView dataview, PrintDocument printDoc,string footer) this.footer = footer; this.dataview = dataview; this.printDoc = printDoc; PageIndex = 0; /获取打印数据的具体行数 this.rows = dataview.RowCount; this.columns = dataview.ColumnCount; /判断打印设置是否是横向打印 if (!printDoc.DefaultPageSettings.Landscape) PageWidth = printDoc.DefaultPageSettings.PaperSize.Width; PageHeight = printDoc.DefaultPageSettings.PaperSize.Height; else PageHeight = printDoc.DefaultPageSettings.PaperSize.Width; PageWidth = printDoc.DefaultPageSettings.PaperSize.Height; LeftMargin = printDoc.DefaultPageSettings.Margins.Left-50; TopMargin = printDoc.DefaultPageSettings.Margins.Top+60; RightMargin = printDoc.DefaultPageSettings.Margins.Right; BottomMargin = printDoc.DefaultPageSettings.Margins.Bottom-100; height = PageHeight - TopMargin - BottomMargin - 2; width = PageWidth - LeftMargin - RightMargin - 2; double tempheight = height; double temprowHeight = rowHeight; while (true) string temp = Convert.ToString(tempheight / Math.Round(temprowHeight, 3); int i = temp.IndexOf(.); double tt = 100; if (i != -1) tt = Math.Round(Convert.ToDouble(temp.Substring(temp.IndexOf(.), 3); if (tt = 0.01) rowHeight = Convert.ToInt32(temprowHeight); break; else temprowHeight = temprowHeight + 0.01; pageSize = height / rowHeight; if (rows + 1) = pageSize) pageSize = rows + 1; PageNumber = 1; else PageNumber = rows / (pageSize - 1); if (rows % (pageSize - 1) != 0) PageNumber = PageNumber + 1; /*/ / 初始化打印 / private void InitPrint() PageIndex = PageIndex + 1; if (PageIndex = PageNumber) hasMorePage = false; if (PageIndex != 1) pageSize = rows % (pageSize - 1) + 1; else hasMorePage = true; /打印头 private void DrawHeader(Graphics g) Font font = new Font(宋体, 11, FontStyle.Regular); int temptop = (rowHeight / 2) + TopMargin + 1; int templeft = LeftMargin + 1; for (int i = 0; i this.columns; i+) string headString = this.dataview.Columnsi.HeaderText; float fontHeight = g.MeasureString(headString, font).Height; float fontwidth = g.MeasureString(headString, font).Width; float temp = temptop - (fontHeight) / 3; g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp); templeft = templeft + (int)(this.dataview.Columnsi.Width / Rate) + 1; /画表格 private void DrawTable(Graphics g) Rectangle border = new Rectangle(LeftMargin, TopMargin, width, (pageSize) * rowHeight); g.DrawRectangle(new Pen(Brushes.Black, 1), border); for (int i = 1; i pageSize; i+) if (i != 1) g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1); else g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1); /计算出列的总宽度和打印纸比率 Rate = Convert.ToDouble(GetDateViewWidth() / Convert.ToDouble(width); int tempLeft = LeftMargin + 1; int endY = (pageSize) * rowHeight + TopMargin; for (int i = 1; i columns; i+) tempLeft = tempLeft + 1 + (int)(this.dataview.Columnsi - 1.Width / Rate); g.DrawLine(new Pen(Brushes.Black, 1), new Point(tempLeft, TopMargin), new Point(tempLeft, endY); /*/ / 获取打印的列的总宽度 / / private int GetDateViewWidth() int total = 0; for (int i = 0; i this.columns; i+) total = total + this.dataview.Columnsi.Width; return total; /打印行数据 private void DrawRows(Graphics g) Font font = new Font(宋体, 11, FontStyle.Regular); int temptop = (rowHeight / 2) + TopMargin + 1 + rowHeight; for (int i = currRow; i pageSize + currRow - 1; i+) int templeft = LeftMargin + 1; for (int j = 0; j columns; j+) string headString = this.dataview.Rowsi.Cellsj.Value.ToString(); float fontHeight = g.MeasureString(headString, font).Height; float fontwidth = g.MeasureString(headString, font).Width; float temp = temptop - (fontHeight) / 3; while (true) if (fontwidth = (int)(this.dataview.Columnsj.Width / Rate) break; else headString = headString.Substring(0, headString.Length - 1); fontwidth = g.MeasureString(headString, font).Width; g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp); templeft = templeft + (int)(this.dataview.Columnsj.Width / Rate) + 1; temptop = temptop + rowHeight; currRow = pageSize + currRow - 1; AreaHeight = temptop; /*/ / 在PrintDocument中的PrintPage方法中调用 / / 传入PrintPage中PrintPageEventArgs中的Graphics / 是否还有打印页 有返回true,无则返回false public bool Print(Graphics g) InitPrint(); DrawTable(g); DrawHeader(g); DrawRows(g); /打印页码 string pagestr = PageIndex + /

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论