C#多功能DataGridView打印类(WinForm)

摘要:
publicinttopMargin=30//顶边距publicintcellTopMargin=6;//单元格顶边距publicintcellLeftMargin=4;//单元格左边距publiccharsplitChar='#';//当header要用斜线表示的时候publicstringfalseStr="×";//如果传进来的DataGridView中有false,把其转换得字符。publicstringtrueStr="√";//如果传进来的DataGridView中有true,把其转换得字符。publicintpageRowCount=30;//每页行数publicintrowGap=28;//行高publicintcolGap=5;//每列间隔publicintleftMargin=45;//左边距publicFonttitleFont=newFont;//标题字体publicFontfont=newFont;//正文字体publicFontheaderFont=newFont;//列名标题publicFontfooterFont=newFont;//页脚显示页数的字体publicFontupLineFont=newFont;//当header分两行显示的时候,上行显示的字体。publicFontunderLineFont=newFont;//当header分两行显示的时候,下行显示的字体。

usingSystem;
using
System.Collections;
using
System.ComponentModel;
using
System.Drawing;
using
System.Drawing.Printing;
using
System.Data;
using
System.Windows.Forms;
namespace
WeightSystem
{
publicclass
cutePrinter2
{
private
DataGridView dataGridView1;
private
PrintDocument printDocument;
private
PageSetupDialog pageSetupDialog;
private
PrintPreviewDialog printPreviewDialog;
privatestringtitle =""
;
intcurrentPageIndex =0
;
introwCount =0
;
intpageCount =0
;
inttitleSize =20
;
boolisCustomHeader =false
;
Brush alertBrush
=new
SolidBrush(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
=new
PrintDocument();
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(object
sender, 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), new
Point(rightBound, y), e.Graphics);
y
+=
rowGap;
}
DrawLine(
newPoint(leftMargin, y), new
Point(rightBound, y), e.Graphics);
currentPageIndex
++
;
if(this.setTongji &&currentPageIndex ==
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
=new
Pen(Color.Black);
gp.DrawLine(pen, sp, ep);
}
public
PrintDocument GetPrintDocument()
{
return
printDocument;
}
publicvoid
Print()
{
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
=new
PageSetupDialog();
pageSetupDialog.AllowOrientation
=true
;
pageSetupDialog.Document
=
printDocument;
pageSetupDialog.Document.DefaultPageSettings.Landscape
=true; //设置打印为横向
pageSetupDialog.ShowDialog();
printPreviewDialog
=new
PrintPreviewDialog();
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)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇wiki部署WEB前端组件思想【日历】下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【原创】进销存快速开发框架 (Winform三层架构+DevExpress+MsSQL)

进销存系统简介 进销存,又称为购销链,是指企业管理过程中采购进货 (进)--> 入库存货(存)-->销售出库(销)三个主要流程环节的动态管理过程。   进:指询价、采购到入库与付款的过程。 销:指报价、销售到出库与收款的过程。 存:指出入库之外,包括领料、退货、盘点、损益、借入、借出、调拨等影响库存数量的动作。     进销存软件是对企业生产经...

Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值

场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100540708 在上面实现了将RadioGroup的选项根据配置文件动态配置后, 比如这里有三个选项,在更改选项时会对其他的控件的值进行...

.Net WinForm下配置Log4Net(总结不输出原因)

最近做一个winform项目,配置了Log4net 但是总是不能输出,搜索了很多文章加上自己的探索发现自己在项目中添加的Log4Net.config 生成时没有被复制到Debug文件夹下, 所以程序在调用日志输出时找不到这个配置文件,所以没有输出(在网上搜了很多,也有很多说是路径问题造成不能输出) 解决办法:1.在项目工程中,选中Log4Net.confi...

WinForm实现简单的拖拽功能(C#)

用到了ListBox和TreeView两个控件,ListBox作为数据源,通过拖拽其中的数据放置到TreeView上,自动添加一个树节点 ListBox控件的MouseDown用于获取要拖拽的值并调用DoDragDrop方法 privatevoidlistBox1_MouseDown(objectsender,MouseEventArgse){//调用Do...

WinForm窗体传值 总结

      了解了窗体的显示相关知识,接着总结一下窗体的传值方法: 1.通过构造函数特点:传值是单向的(不可以互相传值),实现简单实现代码如下:在窗体Form2中       int value1;string value2; public Form2 ( int value1 , string value2 ){    InitializeCompo...

C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumnCheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].FormattedValue.ToString()=...