
能实现如上图的的打印功能。
·所有字体,边距,header 高,行高,都可以自定义。
·支持自动计算每页行数与每页固定行数。
·支持页脚显示页数。
由于自己用和本人比较懒,所以把属性都设置成公有,赋值的时候小心。
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
namespace cjManager
{
public class cutePrinter
{
private DataGrid dataGrid;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
private string title="";
int currentPageIndex=0;
int rowCount=0;
int pageCount=0;
int titleSize=16;
bool isCustomHeader=false;
//Brush alertBrush=new SolidBrush(Color.Red);
string[] header=null;//如果自定义就填入字符串,如果需要斜线分隔,就用\表示,例如:个数#名字 其中#为splitChar
string[] uplineHeader=null;//上行文字数组
int[] upLineHeaderIndex=null;//上行的文字index,如果没有上行就设为-1;
//bool isEveryPagePrintHead=true;//是否每一页都要打印列头。
public bool isEveryPagePrintTitle=false;//是否每一页都要打印标题。
public int headerHeight=50;//标题高度。
public int topMargin=60; //顶边距
public int cellTopMargin=6;//单元格顶边距
public int cellLeftMargin=4;//单元格左边距
public char splitChar='#';//当header要用斜线表示的时候
public string falseStr="×";//如果传进来的dataGrid中有 false,把其转换得字符。
public string trueStr="√";//如果传进来的dataGrid中有 true,把其转换得字符。
public int pageRowCount=7;//每页行数
public int rowGap = 30;//行高
public int colGap = 5;//每列间隔
public int leftMargin = 50;//左边距
public Font titleFont=new Font("Arial",14);//标题字体
public Font font = new Font("Arial", 10);//正文字体
public Font headerFont = new Font("Arial", 9, FontStyle.Bold);//列名标题
public Font footerFont=new Font("Arial",8);//页脚显示页数的字体
public Font upLineFont=new Font("Arial",9, FontStyle.Bold);//当header分两行显示的时候,上行显示的字体。
public Font underLineFont=new Font("Arial",8);//当header分两行显示的时候,下行显示的字体。
public Brush brush = new SolidBrush(Color.Black);//画刷
public bool isAutoPageRowCount=true;//是否自动计算行数。
public int buttomMargin=80;//底边距
public bool needPrintPageIndex=true;//是否打印页脚页数
//string filterStr="";
public cutePrinter(DataGrid dataGrid,string title,int titleSize)
{
this.title=title;
//this.titleSize=titleSize;
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public cutePrinter(DataGrid dataGrid,string title)
{
this.title=title;
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public cutePrinter(DataGrid dataGrid)
{
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public bool setTowLineHeader(string[] upLineHeader,int[] upLineHeaderIndex)
{
this.uplineHeader=upLineHeader;
this.upLineHeaderIndex=upLineHeaderIndex;
this.isCustomHeader=true;
return true;
}
public bool setHeader(string[] header)
{
this.header=header;
return true;
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int width=e.PageBounds.Width;
int height=e.PageBounds.Height;
if(this.isAutoPageRowCount)
pageRowCount=(int)((height-this.topMargin-titleSize-this.headerFont.Height-this.headerHeight-this.buttomMargin)/this.rowGap);
pageCount=(int)(rowCount/pageRowCount);
if(rowCount%pageRowCount>0)
pageCount++;
int xoffset=(int)((width-e.Graphics.MeasureString(this.title,this.titleFont).Width)/2);
int colCount = 0;
int x = 0;
int y =topMargin;
string cellValue = "";
int startRow=currentPageIndex*pageRowCount;
int endRow=startRow+this.pageRowCount
1<rowcount?startrow+pagerowcount:rowcount; +="rowGap;" <="" colcount="dataGrid.TableStyles[0].GridColumnStyles.Count;" colcount;="" colwidth="" currentpagerowcount="endRow-startRow;" drawline(new="" e.graphics.drawstring(this.title,titlefont,brush,xoffset,y);="" for(int="" if(="" if(this.currentpageindex="0" indexj="-1;" int="" j="" j++)="" lastindex="-1;" lastlength="0;" point(x,y),new="" point(x,y+currentpagerowcount*rowgap+this.headerheight),e.graphics);="" this.iseverypageprinttitle)="" x="leftMargin;" y="" y+="titleSize;" {="" ||="" }="" 最左边的竖线=""> 0)
2{
3indexj++;
4if(this.header==null || this.header[indexj]=="")
5cellValue = dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;
6else
7cellValue=header[indexj];
8
9if(this.isCustomHeader)
10{
11if(this.upLineHeaderIndex[indexj]!=lastIndex)
12{
13
14if(lastLength>0 && lastIndex>-1)//开始画上一个upline
15{
16string upLineStr=this.uplineHeader[lastIndex];
17int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
18if(upXOffset<0)
19upXOffset=0;
20e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2));
21
22DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
23DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
24}
25lastIndex=this.upLineHeaderIndex[indexj];
26lastLength=colWidth+colGap;
27}
28else
29{
30lastLength+=colWidth+colGap;
31}
32}
33
34//int currentY=y+cellTopMargin;
35
36
37int Xoffset=10;
38int Yoffset=20;
39int leftWordIndex=cellValue.IndexOf(this.splitChar);
40if(this.upLineHeaderIndex!=null && this.upLineHeaderIndex[indexj]>-1)
41{
42
43if(leftWordIndex>0)
44{
45string leftWord=cellValue.Substring(0,leftWordIndex);
46string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
47//上面的字
48Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
49Yoffset=(int)(this.headerHeight/2-e.Graphics.MeasureString("a",this.underLineFont).Height);
50
51
52//Xoffset=6;
53//Yoffset=10;
54e.Graphics.DrawString(rightWord,this.underLineFont,brush,x+Xoffset-4,y+(int)(this.headerHeight/2));
55e.Graphics.DrawString(leftWord,this.underLineFont,brush,x+2,y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2)+Yoffset-2);
56DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
57x += colWidth + colGap;
58DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
59}
60else
61{
62
63e.Graphics.DrawString(cellValue, headerFont, brush, x, y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2));
64x += colWidth + colGap;
65DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
66}
67
68}
69else
70{
71if(leftWordIndex>0)
72{
73string leftWord=cellValue.Substring(0,leftWordIndex);
74string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
75//上面的字
76Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
77Yoffset=(int)(this.headerHeight-e.Graphics.MeasureString("a",this.underLineFont).Height);
78
79e.Graphics.DrawString(rightWord,this.headerFont,brush,x+Xoffset-4,y+2);
80e.Graphics.DrawString(leftWord,this.headerFont,brush,x+2,y+Yoffset-4);
81DrawLine(new Point(x,y),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
82x += colWidth + colGap;
83DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
84}
85else
86{
87e.Graphics.DrawString(cellValue, headerFont, brush, x, y+cellTopMargin);
88x += colWidth + colGap;
89DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
90}
91
92}
93
94}
95}
96////循环结束,画最后一个的upLine
97if(this.isCustomHeader)
98{
99
100if(lastLength>0 && lastIndex>-1)//开始画上一个upline
101{
102string upLineStr=this.uplineHeader[lastIndex];
103int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
104if(upXOffset<0)
105upXOffset=0;
106e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2));
107
108DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
109DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
110}
111
112}
113
114int rightBound=x;
115
116DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics); //最上面的线
117
118//DrawLine(new Point(leftMargin,y+this.headerHeight),new Point(rightBound,y+this.headerHeight),e.Graphics);//列名的下面的线
119
120y+=this.headerHeight;
121
122
123//print all rows
124for(int i = startRow; i < endRow; i++)
125{
126
127x = leftMargin;
128for(int j = 0; j < colCount; j++)
129{
130if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
131{
132cellValue = dataGrid[i,j].ToString();
133if(cellValue=="False")
134cellValue=falseStr;
135if(cellValue=="True")
136cellValue=trueStr;
137
138e.Graphics.DrawString(cellValue, font, brush, x+this.cellLeftMargin, y+cellTopMargin);
139x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
140y = y + rowGap * (cellValue.Split(new char[] {'\r', '\n'}).Length - 1);
141}
142}
143DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics);
144y += rowGap;
145}
146DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics);
147
148currentPageIndex++;
149
150if(this.needPrintPageIndex)
151e.Graphics.DrawString("共 "+pageCount.ToString()+" 页,当前第 "+this.currentPageIndex.ToString()+" 页",this.footerFont,brush,width-200,(int)(height-this.buttomMargin/2-this.footerFont.Height));
152
153string s = cellValue;
154string f3 = cellValue;
155
156
157
158if(currentPageIndex<pageCount)
159{
160e.HasMorePages=true;
161}
162else
163{
164e.HasMorePages=false;
165this.currentPageIndex=0;
166
167}
168//iPageNumber+=1;
169
170
171}
172private void DrawLine(Point sp,Point ep,Graphics gp)
173{
174Pen pen=new Pen(Color.Black);
175gp.DrawLine(pen,sp,ep);
176}
177
178public PrintDocument GetPrintDocument()
179{
180return printDocument;
181}
182
183public void Print()
184{
185
186
187
188rowCount=0;
189
190if(dataGrid.DataSource.GetType().ToString() == "System.Data.DataTable")
191{
192rowCount = ((DataTable)dataGrid.DataSource).Rows.Count;
193}
194else if(dataGrid.DataSource.GetType().ToString() == "System.Collections.ArrayList")
195{
196rowCount = ((ArrayList)dataGrid.DataSource).Count;
197}
198
199
200try
201{
202pageSetupDialog = new PageSetupDialog();
203pageSetupDialog.Document = printDocument;
204pageSetupDialog.ShowDialog();
205
206printPreviewDialog = new PrintPreviewDialog();
207printPreviewDialog.Document = printDocument;
208printPreviewDialog.Height = 600;
209printPreviewDialog.Width = 800;
210
211printPreviewDialog.ShowDialog();
212}
213catch(Exception e)
214{
215throw new Exception("Printer error." + e.Message);
216}
217
218}
219}
220}
221
222//用法示例,显示结果如顶图。
223
224private void bnPrint_Click(object sender, System.EventArgs e)
225{
226
227cutePrinter dgp=new cutePrinter(this.dataGrid1,this.dlSearchYear.Text+"年"+"专业",16);
228string[] uplinestr={"呵呵,hehe","xixi"};
229string[] header={"呵呵#xixi","Hee#xcc","kdfj#djjj","kk#jj","kdjf","","","",""};
230dgp.setHeader(header);//如果不用改原header就不用赋值。
231//注意,这里的列不包括width==0的列
232int[] uplineindex={-1,-1,0,0,0,-1,1,1};//注意,这里的列不包括width==0的列
233dgp.setTowLineHeader(uplinestr,uplineindex);
234dgp.Print();
235}</rowcount?startrow+pagerowcount:rowcount;>