服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用.net自带的类实现datagrid报表的打印


  using system;
  using system.windows.forms;
  using system.drawing;
  using system.drawing.printing;
  using system.data;
  using system.collections;
  using datalibrary;
  
  namespace controllibrary
  {
   ///
   /// datagrid打印
   ///

   public class datagridprinter
   {
   private printdocument theprintdocument;
   private datatable thetable;
   private datagrid thedatagrid;
   public int rowcount = 0;
   private const int kverticalcellleeway = 10;
   public int pagenumber = 1;
   public arraylist lines = new arraylist();
   public int header=0;
   public int footer=0;
   datacatena datacatena1;
   public int pagewidth;
   public int pageheight;
   public int topmargin;
   public int bottommargin;
   int gridwidth = 0;
   public int leftmargin;
   public int rightmargin;
   public string papername;
   public int pagecount;
   private int rows_page;
   public bool showfooter;
  
   public int bodylength
   {
    get
    {
    if(this.pagenumber     return (1+this.rows_page) * ((int)(thedatagrid.font.sizeinpoints) + kverticalcellleeway);
    else
     return (this.thetable.rows.count + 1 -(this.rows_page*(this.pagenumber-1))) * ( (int)
    (thedatagrid.font.sizeinpoints) + kverticalcellleeway);
    }
   }
  
   public datagridprinter(datagrid agrid, printdocument aprintdocument,string theform,int header,int footer)
   {
    //
    // todo: add constructor logic here
    //
    this.header=header;
    this.footer=footer;
    this.showfooter=true;
    thedatagrid = agrid;
    theprintdocument = aprintdocument;
    if(theform!="")
    {
    datacatena1=new datacatena();
    dataview mydv=datacatena1.getdataview("select * from yh_bbdy where bm='"+theform +"'");
  
    this.papername=mydv[0]["zm"].tostring();
  
    papersize pksize;
  
    if(mydv[0]["fx"].tostring()=="true")
     this.theprintdocument.defaultpagesettings.landscape=true;
    else
     this.theprintdocument.defaultpagesettings.landscape=false;
  
    int found=0;
    for (int i = 0; i < this.theprintdocument.printersettings.papersizes.count; i++)
    {
     pksize = this.theprintdocument.printersettings.papersizes[i];
     if(pksize.papername==this.papername)
     {
     this.theprintdocument.defaultpagesettings.papersize=pksize;
     found=1;
     i=this.theprintdocument.printersettings.papersizes.count;
     if(this.theprintdocument.defaultpagesettings.landscape)
     {
      pageheight = pksize.width;
      pagewidth = pksize.height;
     }
     else
     {
      pagewidth = pksize.width;
      pageheight = pksize.height;
     }
     }
    }
  
    if(found==0)
    {
     if(this.theprintdocument.defaultpagesettings.landscape)
     {
     pageheight = int32.parse(mydv[0]["zk"].tostring());
     pagewidth = int32.parse(mydv[0]["zc"].tostring());
     }
     else
     {
     pagewidth = int32.parse(mydv[0]["zk"].tostring());
     pageheight = int32.parse(mydv[0]["zc"].tostring());
     }
    }
  
    topmargin = int32.parse(mydv[0]["sk"].tostring());
    bottommargin = int32.parse(mydv[0]["xk"].tostring());
    rightmargin = int32.parse(mydv[0]["rightk"].tostring());
    leftmargin = int32.parse(mydv[0]["leftk"].tostring());
    }
    else
    {
    pagewidth = theprintdocument.defaultpagesettings.papersize.width;
    pageheight = theprintdocument.defaultpagesettings.papersize.height;
    topmargin = theprintdocument.defaultpagesettings.margins.top;
    bottommargin = theprintdocument.defaultpagesettings.margins.bottom;
    rightmargin = theprintdocument.defaultpagesettings.margins.right;
    leftmargin = theprintdocument.defaultpagesettings.margins.left;
    }
  
    pagewidth -= this.leftmargin ;
    pagewidth -= this.rightmargin;
  
    this.rows_page=(int)((float)(this.pageheight-this.topmargin-this.bottommargin-this.footer-this.header)
    /(thedatagrid.font.sizeinpoints + kverticalcellleeway));
  
    for (int k = 0; k < thedatagrid.tablestyles[0].gridcolumnstyles.count; k++)
    {
    gridwidth += thedatagrid.tablestyles[0].gridcolumnstyles[k].width; // thetable.columns[k].tostring();
    }
  
   }
  
   public void drawheader(graphics g)
   {
    solidbrush forebrush = new solidbrush(thedatagrid.headerforecolor);
    solidbrush backbrush = new solidbrush(color.white);  //(thedatagrid.headerbackcolor);
    pen thelinepen = new pen(thedatagrid.gridlinecolor, 1);
    stringformat cellformat = new stringformat();
    cellformat.trimming = stringtrimming.ellipsischaracter;
    cellformat.formatflags = stringformatflags.nowrap | stringformatflags.linelimit;
    cellformat.alignment= stringalignment.center;
  
  
    int columnwidth = 0;
  
    int initialrowcount = rowcount;
  
    // draw the table header
    float startxposition = this.leftmargin;           //thedatagrid.location.x;
    rectanglef nextcellbounds = new rectanglef(0,0, 0, 0);
  
    /*
     这一段是画表头底色的代码
  
     rectanglef headerbounds = new rectanglef(0, 0, 0, 0);
  
     headerbounds.x = this.leftmargin;    //thedatagrid.location.x;
     headerbounds.y = + (rowcount - initialrowcount) * (thedatagrid.font.sizeinpoints + kverticalcellleeway);
     headerbounds.height = thedatagrid.font.sizeinpoints + kverticalcellleeway;
     headerbounds.width = pagewidth;
    */
    float y1=(float)(header+this.topmargin);
  
    g.drawline(thelinepen, this.leftmargin, y1, this.leftmargin + this.pagewidth, y1);
    y1=y1+ (float)(thedatagrid.font.sizeinpoints + kverticalcellleeway);
    g.drawline(thelinepen, this.leftmargin, y1, this.leftmargin + this.pagewidth, y1);
  
    //  g.fillrectangle(backbrush, headerbounds);
  
    for (int k = 0; k < this.thedatagrid.tablestyles[0].gridcolumnstyles.count; k++)
    {
    columnwidth = thedatagrid.tablestyles[0].gridcolumnstyles[k].width * pagewidth / this.gridwidth;
  
    string nextcolumn = thedatagrid.tablestyles[0].gridcolumnstyles[k].headertext; 
    // thetable.columns[k].tostring(); + (rowcount - initialrowcount) * (thedatagrid.font.sizeinpoints 
    + kverticalcellleeway)
    rectanglef cellbounds = new rectanglef(startxposition, header + topmargin+2 ,
     columnwidth,
     thedatagrid.headerfont.sizeinpoints + kverticalcellleeway-2);
    nextcellbounds = cellbounds;
  
    if (startxposition + columnwidth <= pagewidth+this.leftmargin)
    {
     g.drawstring(nextcolumn, thedatagrid.headerfont, forebrush, cellbounds, cellformat);
    }
  
    startxposition = startxposition + columnwidth;
  
    }
    y1=y1+ (float)(thedatagrid.font.sizeinpoints + kverticalcellleeway);
    g.drawline(thelinepen, this.leftmargin, y1, this.leftmargin + this.pagewidth, y1);
  
    //if (thedatagrid.gridlinestyle != datagridlinestyle.none)
    // g.drawline(thelinepen, thedatagrid.locatio

扫描关注微信公众号