usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Drawing.Printing;
usingSystem.Data;
usingSystem.Windows.Forms;
namespaceWeightSystem
{
publicclasscutePrinter2
{
privateDataGridView dataGridView1;
privatePrintDocument printDocument;
privatePageSetupDialog pageSetupDialog;
privatePrintPreviewDialog printPreviewDialog;
privatestringtitle ="";
intcurrentPageIndex =0;
introwCount =0;
intpageCount =0;
inttitleSize =20;
boolisCustomHeader =false;
Brush alertBrush =newSolidBrush(Color.Red);
string[] header =null; //如果自定义就填入字符串,如果需要斜线分隔,就用表示,例如:个数#名字 其中#为splitChar string[] uplineHeader =null; //上行文字数组 int[] upLineHeaderIndex =null; //上行的文字index,如果没有上行就设为-1;
publicboolisEveryPagePrintTitle =true; //是否每一页都要打印标题。 publicintheaderHeight =30; //标题高度。 publicinttopMargin =30//顶边距 publicintcellTopMargin =6; //单元格顶边距 publicintcellLeftMargin =4; //单元格左边距 publiccharsplitChar ='#'; //当header要用斜线表示的时候 publicstringfalseStr ="×"; //如果传进来的DataGridView中有 false,把其转换得字符。 publicstringtrueStr ="√"; //如果传进来的DataGridView中有 true,把其转换得字符。 publicintpageRowCount =30; //每页行数 publicintrowGap =28; //行高 publicintcolGap =5; //每列间隔 publicintleftMargin =45; //左边距 publicFont titleFont =newFont("黑体"24, FontStyle.Bold); //标题字体 publicFont font =newFont("宋体"10); //正文字体 publicFont headerFont =newFont("黑体"11, FontStyle.Bold); //列名标题 publicFont footerFont =newFont("Arial"8); //页脚显示页数的字体 publicFont upLineFont =newFont("Arial"9, FontStyle.Bold); //当header分两行显示的时候,上行显示的字体。 publicFont underLineFont =newFont("Arial"8); //当header分两行显示的时候,下行显示的字体。 publicBrush brush =newSolidBrush(Color.Black); //画刷 publicboolisAutoPageRowCount =true; //是否自动计算行数。 publicintbuttomMargin =80; //底边距 publicboolneedPrintPageIndex =true; //是否打印页脚页数 publicboolsetTongji =false; //设置是否显示统计
stringsTitle =""; //显示标题stringsTongJi01 =""; //统计01stringsTongJi02 =""; //统计02stringsTongJi03 =""; //统计03boolisTongji =false; //是否显示统计
stringtime01; //具体时间标题stringtime02;
Font tongJiFont =newFont("宋体"14); //统计Font dateFont =newFont("宋体"12, FontStyle.Bold); //日期标题
/// <summary>
/// 日统计报表打印
/// </summary>
/// <param name="dGView">DataGridView</param>
/// <param name="title">标题</param>
/// <param name="times01">起始时间</param>
/// <param name="times02">中止时间</param>
/// <param name="tj01">统计01</param>
/// <param name="tj02">统计02</param>
/// <param name="tj03">统计03</param>
/// <param name="tj">确认是否打印统计</param>
/// <param name="iPrintType">打印样式选择,默认2</param>publiccutePrinter2(DataGridView dGView, stringtitle, stringtimes01, stringtimes02, stringtj01, stringtj02, stringtj03, booltj)
{
this.title =title;
this.sTongJi01 =tj01;
this.sTongJi02 =tj02;
this.sTongJi03 =tj03;
this.time01 =times01;
this.time02 =times02;
this.setTongji =tj;
this.dataGridView1 =dGView;
printDocument =newPrintDocument();
printDocument.PrintPage +=newPrintPageEventHandler(this.printDocument_PrintPage);
}
publicboolsetTowLineHeader(string[] upLineHeader, int[] upLineHeaderIndex)
{
this.uplineHeader =upLineHeader;
this.upLineHeaderIndex =upLineHeaderIndex;
this.isCustomHeader =true;
returntrue;
}
publicboolsetHeader(string[] header)
{
this.header =header;
returntrue;
}
privatevoidprintDocument_PrintPage(objectsender, System.Drawing.Printing.PrintPageEventArgs e)
{
#region打印 --- 统计报表(年报表)
intwidth =e.PageBounds.Width;
intheight =e.PageBounds.Height;
this.leftMargin =40; //重新设置左边距
if(this.isAutoPageRowCount)
{
pageRowCount =(int)((height -this.topMargin -titleSize -25-this.headerFont.Height -this.headerHeight -this.buttomMargin) /this.rowGap);
}
pageCount =(int)(rowCount /pageRowCount);
if(rowCount %pageRowCount >0)
pageCount++;
if(this.setTongji &&pageCount ==1)
{
pageRowCount =(int)((height -this.topMargin -titleSize -25-this.headerFont.Height -this.headerHeight -this.buttomMargin -25/this.rowGap);
pageCount =(int)(rowCount /pageRowCount);
if(rowCount %pageRowCount >0)
pageCount++;
}
stringsDateTitle =time01 +"— "+time02;
intxoffset =(int)((width -e.Graphics.MeasureString(this.title, this.titleFont).Width) /2);
intxoffset2 =(int)((width -e.Graphics.MeasureString(sDateTitle, dateFont).Width) /2);
intcolCount =0;
intx =0;
inty =topMargin;
stringcellValue ="";
intstartRow =currentPageIndex *pageRowCount;
intendRow =startRow +this.pageRowCount <rowCount ?startRow +pageRowCount : rowCount;
intcurrentPageRowCount =endRow -startRow;
if(this.currentPageIndex ==0||this.isEveryPagePrintTitle)
{
e.Graphics.DrawString(this.title, titleFont, brush, xoffset, y);
e.Graphics.DrawString(sDateTitle, dateFont, brush, xoffset2, y +40);
y +=titleSize +20;
}
try
{
colCount =dataGridView1.ColumnCount;
y +=rowGap;
x =leftMargin;
DrawLine(newPoint(x, y), newPoint(x, y +currentPageRowCount *rowGap +this.headerHeight), e.Graphics); //最左边的竖线
intlastIndex =-1;
intlastLength =0;
intindexj =-1;
for(intj =0; j <colCount; j++)
{
intcolWidth =dataGridView1.Columns[j].Width;
if(colWidth >0)
{
indexj++;
if(this.header ==null||this.header[indexj] =="")
cellValue =dataGridView1.Columns[j].Name;
else
cellValue =header[indexj];
if(this.isCustomHeader)
{
if(this.upLineHeaderIndex[indexj] !=lastIndex)
{
if(lastLength >0&&lastIndex >-1)//开始画上一个upline {
stringupLineStr =this.uplineHeader[lastIndex];
intupXOffset =(int)((lastLength -e.Graphics.MeasureString(upLineStr, this.upLineFont).Width) /2);
if(upXOffset <0)
upXOffset =0;
e.Graphics.DrawString(upLineStr, this.upLineFont, brush, x -lastLength +upXOffset, y +(int)(this.cellTopMargin /2));
DrawLine(newPoint(x -lastLength, y +(int)(this.headerHeight /2)), newPoint(x, y +(int)(this.headerHeight /2)), e.Graphics); //中线 DrawLine(newPoint(x, y), newPoint(x, y +(int)(this.headerHeight /2)), e.Graphics);
}
lastIndex =this.upLineHeaderIndex[indexj];
lastLength =colWidth +colGap;
}
else
{
lastLength +=colWidth +colGap;
}
}
//int currentY=y+cellTopMargin;
intXoffset =10;
intYoffset =20;
intleftWordIndex =cellValue.IndexOf(this.splitChar);
if(this.upLineHeaderIndex !=null&&this.upLineHeaderIndex[indexj] >-1)
{
if(leftWordIndex >0)
{
stringleftWord =cellValue.Substring(0, leftWordIndex);
stringrightWord =cellValue.Substring(leftWordIndex +1, cellValue.Length -leftWordIndex -1);
//上面的字 Xoffset =(int)(colWidth +colGap -e.Graphics.MeasureString(rightWord, this.upLineFont).Width);
Yoffset =(int)(this.headerHeight /2-e.Graphics.MeasureString("a"this.underLineFont).Height);
Xoffset =6;
Yoffset =10;
e.Graphics.DrawString(rightWord, this.underLineFont, brush, x +Xoffset -4, y +(int)(this.headerHeight /2));
e.Graphics.DrawString(leftWord, this.underLineFont, brush, x +2, y +(int)(this.headerHeight /2+(int)(this.cellTopMargin /2+Yoffset -2);
DrawLine(newPoint(x, y +(int)(this.headerHeight /2)), newPoint(x +colWidth +colGap, y +headerHeight), e.Graphics);
x +=colWidth +colGap;
DrawLine(newPoint(x, y +(int)(this.headerHeight /2)), newPoint(x, y +currentPageRowCount *rowGap +this.headerHeight), e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y +(int)(this.headerHeight /2+(int)(this.cellTopMargin /2));
x +=colWidth +colGap;
DrawLine(newPoint(x, y +(int)(this.headerHeight /2)), newPoint(x, y +currentPageRowCount *rowGap +this.headerHeight), e.Graphics);
}
}
else
{
if(leftWordIndex >0)
{
stringleftWord =cellValue.Substring(0, leftWordIndex);
stringrightWord =cellValue.Substring(leftWordIndex +1, cellValue.Length -leftWordIndex -1);
//上面的字 Xoffset =(int)(colWidth +colGap -e.Graphics.MeasureString(rightWord, this.upLineFont).Width);
Yoffset =(int)(this.headerHeight -e.Graphics.MeasureString("a"this.underLineFont).Height);
e.Graphics.DrawString(rightWord, this.headerFont, brush, x +Xoffset -4, y +2);
e.Graphics.DrawString(leftWord, this.headerFont, brush, x +2, y +Yoffset -4);
DrawLine(newPoint(x, y), newPoint(x +colWidth +colGap, y +headerHeight), e.Graphics);
x +=colWidth +colGap;
DrawLine(newPoint(x, y), newPoint(x, y +currentPageRowCount *rowGap +this.headerHeight), e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y +cellTopMargin);
x +=colWidth +colGap;
DrawLine(newPoint(x, y), newPoint(x, y +currentPageRowCount *rowGap +this.headerHeight), e.Graphics);
}
}
}
}
////循环结束,画最后一个的upLine if(this.isCustomHeader)
{
if(lastLength >0&&lastIndex >-1)//开始画上一个upline {
stringupLineStr =this.uplineHeader[lastIndex];
intupXOffset =(int)((lastLength -e.Graphics.MeasureString(upLineStr, this.upLineFont).Width) /2);
if(upXOffset <0)
upXOffset =0;
e.Graphics.DrawString(upLineStr, this.upLineFont, brush, x -lastLength +upXOffset, y +(int)(this.cellTopMargin /2));
DrawLine(newPoint(x -lastLength, y +(int)(this.headerHeight /2)), newPoint(x, y +(int)(this.headerHeight /2)), e.Graphics); //中线 DrawLine(newPoint(x, y), newPoint(x, y +(int)(this.headerHeight /2)), e.Graphics);
}
}
intrightBound =x;
DrawLine(newPoint(leftMargin, y), newPoint(rightBound, y), e.Graphics); //最上面的线
//DrawLine(new Point(leftMargin,y+this.headerHeight),new Point(rightBound,y+this.headerHeight),e.Graphics); //列名的下面的线
y +=this.headerHeight;
//print all rows for(inti =startRow; i <endRow; i++)
{
x =leftMargin;
for(intj =0; j <colCount; j++)
{
if(dataGridView1.Columns[j].Width >0)
{
cellValue =dataGridView1.Rows[i].Cells[j].Value.ToString();
if(cellValue =="False")
cellValue =falseStr;
if(cellValue =="True")
cellValue =trueStr;
e.Graphics.DrawString(cellValue, font, brush, x +this.cellLeftMargin, y +cellTopMargin);
x +=dataGridView1.Columns[j].Width +colGap;
y =y +rowGap *(cellValue.Split(newchar[] { ''''}).Length -1);
}
}
DrawLine(newPoint(leftMargin, y), newPoint(rightBound, y), e.Graphics);
y +=rowGap;
}
DrawLine(newPoint(leftMargin, y), newPoint(rightBound, y), e.Graphics);
currentPageIndex++;
if(this.setTongji &¤tPageIndex ==pageCount)
this.isTongji =true;
if(this.isTongji)
{
intxoffsetTongJi =(int)((width -e.Graphics.MeasureString(sTongJi01, dateFont).Width) /2);
e.Graphics.DrawString(this.sTongJi01, this.tongJiFont, brush, xoffsetTongJi, y +25); //统计01
e.Graphics.DrawString(this.sTongJi02, this.tongJiFont, brush, xoffsetTongJi, y +50); //统计03e.Graphics.DrawString(this.sTongJi03, this.tongJiFont, brush, xoffsetTongJi +340, y +50); //统计04}
if(this.needPrintPageIndex)
{
if(pageCount !=1)
{
e.Graphics.DrawString(""+pageCount.ToString() +"页,当前第 "+this.currentPageIndex.ToString() +"页"this.footerFont, brush, width -200, (int)(height -this.buttomMargin /2-this.footerFont.Height));
}
}
strings =cellValue;
stringf3 =cellValue;
if(currentPageIndex <pageCount)
{
e.HasMorePages =true;
}
else
{
e.HasMorePages =false;
this.currentPageIndex =0;
}
//iPageNumber+=1;
}
catch
{
}
#endregion
}
privatevoidDrawLine(Point sp, Point ep, Graphics gp)
{
Pen pen =newPen(Color.Black);
gp.DrawLine(pen, sp, ep);
}
publicPrintDocument GetPrintDocument()
{
returnprintDocument;
}
publicvoidPrint()
{
rowCount =0;
try
{
if(dataGridView1.DataSource.GetType().ToString() =="System.Data.DataSet")
{
rowCount =((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count;
}
elseif(dataGridView1.DataSource.GetType().ToString() =="System.Data.DataView")
{
rowCount =((DataView)dataGridView1.DataSource).Count;
}
pageSetupDialog =newPageSetupDialog();
pageSetupDialog.AllowOrientation =true;
pageSetupDialog.Document =printDocument;
pageSetupDialog.Document.DefaultPageSettings.Landscape =true; //设置打印为横向pageSetupDialog.ShowDialog();
printPreviewDialog =newPrintPreviewDialog();
printPreviewDialog.Document =printDocument;
printPreviewDialog.Height =600;
printPreviewDialog.Width =800;
printPreviewDialog.ClientSize =newSystem.Drawing.Size(1024768);
printPreviewDialog.PrintPreviewControl.Zoom =1;
printPreviewDialog.ShowDialog();
}
catch
{
//throw new Exception("Printer error." + e.Message); }
}
}
}
C#多功能DataGridView打印类(WinForm)
免责声明:文章转载自《C#多功能DataGridView打印类(WinForm)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。
宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=