关于C#报表设计中的一点个人经验(winform),本篇文章是借鉴别人的。。。。用于收藏

2.一个报表格式解析器。。
虽然比不上完整的商业报表设计器。。但也够用了。。

另:还有一个Web报表生成器。。逻辑功能已全部完成的。。
    就是说在b/s模式下生成的报表。。完全不用第三方控件!生成的报表效果可满足一般的应用。。
有希望的朋友可给我留言。。

.net打印相关的控件
  通常在打印作业中,用户会有打印页面设置,打印机设置,打印预览,打印等几个常用的
操作.在.net 中有相应的控件对应上述的应用操作.
  1.页面设置,使用pagesetupDialog控件
  2.打印机设置,使用PrintDialog控件
  3.打印预览,PrintpreviewDialog控件
  4.打印,利用PrintDocument.print()进行打印..
  其它控件较易理解,在此主要说一下printDocument控件.
  PrintDocument 控件本质的说,它是一个指向内存区域的指针.
  该内存区域存放的就是要打印的内容.而且其中包括页面格式,如横印,直印,A3纸,A4  
纸设置等,
  printdocument控件也可以理解是一张"白纸",你可以在上面写上任何东东..比如文字
,  图片,各种图案.等等..你写上什么,它就打印什么..有一个很重要的"动作"要注意.


就是在什么时候"写上"东东呢..这个要写的"时间"就是用printpage事件..
  printpage事件,表示当要在printdocument写东东时,会触发该事件..然后你可以在该
事件放上你要写的东东.比如在 :

private void printDocument1_PrintPage(object sender, 
System.Drawing.Printing.PrintPageEventArgs e)
  {
  Graphics g = e.Graphics; //获得绘图对象,
  g.DrawLine(myPen,0,0,10,0); //表示在打印时画一直线
} 

上述代码打印时,就只是在"纸上"(printdocument)显示一条直线,如想写上文字,再增加
代码.如g.DrawString("测试文字", printFont, myBrush, 10,20, new 
StringFormat());

  当进行页面设置时,首先必须先将printDocument这个控件"绑定"到pagesetupdialog
控件..表示说现在进行页面设置的是对这个PrintDocument设置...这样页面设置完毕后
才可对printDoument有效..
  同样,打印机设置也须对printdocument和printdialog绑定.
  打印预览也须对printpreviewdilog和printdocument绑定..
  
  上面是对.net相关打印控件的简述
  下面分析一下实际情况的打印原理,
  
  假设现在要在页面顶端打印"2008北京奥运" ,加粗,20号字体
  然后在字体下面一横线
接着打印比赛项目.比如排球,网球...(这可能有很多页)
  最后在结束时(也是最后一页),打印总计比赛项目个数.图如下
                        2008北京奥运                
  -------------------------------------------------------
  1.  排球
  2.  网球
  3.  足球 
  ........
  ........
  -------------------------------------------------------
  比赛总项目: xx
  分析:
  第一,先把打印内容写在"纸"上(printdocument的printpage事件)
  1.先在printdocument的顶端写上 "2008北京奥运 "
  2.再画横线.
3.从数据库读取比赛项目.并写上比赛项目
  4.结束后再画一直线,
  5.再打印比赛总项目:xxx
  6.打印完毕.
  第二,把printdocument绑定到pagesetupDialog,实现页面设置
  第三,把printdocument绑定到PrintDialog实现打印机设置
  第四.把printdocument绑定到PrintpreviewDialog实现打印预览
  第五,利用printdocument.print()方法实现直接打印..

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
  Graphics g = e.Graphics; //获得绘图对象
  this.intPage++;
  if(this.intPage==1)
  {
    myHeader.ProDrawReportHeader(e,this.intPage); //报表表头
  }
  myPageHeader.ProDrawPageHeader(e); //报表页头
  
   //float yPosition=myDetail.ProDrawDetail(g,e,tbPublic); //明细,并返回当页最底的位置,以便可以给页尾打印。
  float yPosition=myDetailGroup.ProDrawDetailGroup(g,e,tbPublic,tbPublic2); //打印分组明细
  myPageFooter.ProDrawPageFooter(g,yPosition+20); //报表页尾
  if( myDetailGroup.intnowrow<tbPublic.Rows.Count)
  {
    e.HasMorePages=true;
    
  }
  else
  {
    e.HasMorePages=false;    
    myReportFooter.ProDrawReportHeader(g,yPosition+10); //报表表尾
    myDetailGroup.intnowrow=0;
    this.intPage=0;
  }  
  }
public float ProDrawDetail(System.Drawing.Graphics myGraph,System.Drawing.Printing.PrintPageEventArgs e,DataTable myTable)
  {
  if(myGraph==null)
    return -1;
    float linesPerPage = 0; //页面的行号
    float yPosition = 0; //绘制字符串的纵向位置
    int count = 0; //行计数器
    float leftMargin = e.MarginBounds.Left; //左边距
    float topMargin = e.MarginBounds.Top; //上边距
    string line = null; //行字符串
    Font printFont =new Font("Roman",12);
  SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
    linesPerPage = e.MarginBounds.Height / printFont.GetHeight(myGraph)-4;//每页可打印的行数
  System.Drawing.Pen myPen=new Pen(myBrush);
  //myGraph.DrawRectangle(myPen,leftMargin,topMargin,e.MarginBounds.Width,e.MarginBounds.Height);
  while(count < linesPerPage &&  this.intnowrow<myTable.Rows.Count)
    {
    yPosition = topMargin + (count * printFont.GetHeight(myGraph))+60;
    for(int k=0;k<myTable.Columns.Count-1;k++)
    {
    line=myTable.Rows[this.intnowrow]["c"+k.ToString()].ToString();
    myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
    leftMargin=leftMargin+160;
    }
    //测试附加id
    line=myTable.Rows[this.intnowrow]["id"].ToString();
    myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
    leftMargin=leftMargin+160;
    leftMargin=e.MarginBounds.Left;
    myGraph.DrawLine(myPen,leftMargin,yPosition+2,e.MarginBounds.Width,yPosition+2);
    count++;    
    line="";
    this.intnowrow=this.intnowrow+1;
    } //结束
  myPen.Color=Color.PaleGreen;
  myGraph.DrawLine(myPen,leftMargin,yPosition+2,e.MarginBounds.Width,yPosition+2); //最后一横线
  myPen.Color=Color.DarkTurquoise;
  myGraph.DrawLine(myPen,e.MarginBounds.Left,e.MarginBounds.Top,e.MarginBounds.Left,e.MarginBounds.Height);
  myGraph.DrawLine(myPen,e.MarginBounds.Left+160,e.MarginBounds.Top,e.MarginBounds.Left+160,e.MarginBounds.Height);
  myGraph.DrawLine(myPen,e.MarginBounds.Left+320,e.MarginBounds.Top,e.MarginBounds.Left+320,e.MarginBounds.Height);
  return yPosition;
  
  }
public void ProDrawPageFooter(System.Drawing.Graphics myGraph,float intY)
  {
  if(myGraph==null)
    return;
  string strText="报表页尾";
  Font myFont=new Font("roman",16);
  SolidBrush myBrush=new SolidBrush(Color.Red);
  myGraph.DrawString(strText, myFont, myBrush, 150,intY, new StringFormat());
  //myGraph.Dispose();
  }

public void ProDrawPageHeader(System.Drawing.Printing.PrintPageEventArgs e)
  {
  if(e==null)
    return;
  Graphics myGraph=e.Graphics;
  string strText="报表页头";
  Font myFont=new Font("roman",16);
  SolidBrush myBrush=new SolidBrush(Color.Red);
  myGraph.DrawString(strText, myFont, myBrush, e.MarginBounds.Width/2,70, new StringFormat());
  //myGraph.Dispose();
  }
public void ProDrawReportHeader(System.Drawing.Graphics myGraph,float intY)
  {
  if(myGraph==null)
    return;
  string strText="报表表尾";
  Font myFont=new Font("roman",20);
  SolidBrush myBrush=new SolidBrush(Color.Blue);
  myGraph.DrawString(strText, myFont, myBrush, 250,intY, new StringFormat());
  //myGraph.Dispose();
  }

public void ProDrawReportHeader(System.Drawing.Printing.PrintPageEventArgs e,int intP)
  {
  
  if(e==null)
    return;
  Graphics myGraph=e.Graphics;
  string strText="报表表头 Page:"+intP.ToString();
  Font myFont=new Font("roman",20);  
  SolidBrush myBrush=new SolidBrush(Color.Blue);
  myGraph.DrawString(strText, myFont, myBrush, e.MarginBounds.Width/2,20, new StringFormat());
  Pen myPen=new Pen(Color.Brown,2);
  myGraph.DrawLine(myPen,e.MarginBounds.X,60,e.MarginBounds.Width,60);
  //myGraph.Dispose();
  }
public float ProDrawDetailGroup(System.Drawing.Graphics myGraph,System.Drawing.Printing.PrintPageEventArgs e,DataTable myTable,DataTable myTable2)
  {
  if(myGraph==null)
    return -1;
    float linesPerPage = 0; //页面的行号
    float yPosition = 0; //绘制字符串的纵向位置
    int count = 0; //行计数器
    float leftMargin = e.MarginBounds.Left; //左边距
    float topMargin = e.MarginBounds.Top; //上边距
    string line = null; //行字符串
    Font printFont =new Font("Roman",12);
  SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
    linesPerPage = e.MarginBounds.Height / printFont.GetHeight(myGraph)-4;//每页可打印的行数
  System.Drawing.Pen myPen=new Pen(myBrush);
  //myGraph.DrawRectangle(myPen,leftMargin,topMargin,e.MarginBounds.Width,e.MarginBounds.Height);
  while(count < linesPerPage &&  this.intnowrow<myTable.Rows.Count)
    {
    yPosition = topMargin + (count * printFont.GetHeight(myGraph))+60;
    leftMargin=e.MarginBounds.X;
    printFont=new Font("Roman",12,FontStyle.Bold);
    for(int k=0;k<myTable.Columns.Count-1;k++)
    {
    line=myTable.Rows[this.intnowrow]["c"+k.ToString()].ToString();
    myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
    leftMargin=leftMargin+160;
    }
    //测试附加id
    line=myTable.Rows[this.intnowrow]["id"].ToString();
    myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
    leftMargin=leftMargin+160;
    leftMargin=e.MarginBounds.Left;
    myGraph.DrawLine(myPen,leftMargin,yPosition+2,e.MarginBounds.Width,yPosition+2);
    count++;    
    line="";
    
    //打印副表
    DataView myView=myTable2.DefaultView;
    string strRo="id2='"+myTable.Rows[this.intnowrow]["id"].ToString()+"'";
    myView.RowFilter=strRo;
    printFont =new Font("Roman",12);
    for(int ss=0;ss<myView.Count;ss++)    
    {
    leftMargin=e.MarginBounds.X;
    yPosition = topMargin + (count * printFont.GetHeight(myGraph))+60;
    for(int j=0;j<myTable2.Columns.Count-1;j++)
    {
      line=myView["c2"+j.ToString()].ToString();
      myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
      leftMargin=leftMargin+160;
    }
    line=myView["id2"].ToString();
    myGraph.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
    leftMargin=leftMargin+160;
    count++;
    this.intnowrow2++;
    }
    //结束打印副表
    this.intnowrow=this.intnowrow+1; //父表记录加一
    } //结束
  myPen.Color=Color.PaleGreen;
  myGraph.DrawLine(myPen,leftMargin,yPosition+2,e.MarginBounds.Width,yPosition+2); //最后一横线
  myPen.Color=Color.DarkTurquoise;
  myGraph.DrawLine(myPen,e.MarginBounds.Left,e.MarginBounds.Top,e.MarginBounds.Left,e.MarginBounds.Height);
  myGraph.DrawLine(myPen,e.MarginBounds.Left+160,e.MarginBounds.Top,e.MarginBounds.Left+160,e.MarginBounds.Height);
  myGraph.DrawLine(myPen,e.MarginBounds.Left+320,e.MarginBounds.Top,e.MarginBounds.Left+320,e.MarginBounds.Height);
  return yPosition;
  
  } 

 

原文地址:https://www.cnblogs.com/lvk618/p/BaoB.html