当前位置:编程学习 > Delphi >>

如何用Tprinter设计BCB操作数据库程序中的报表

    经常用Borland C++ Builder5编程,操作数据库时,报表打印是必不可少的。但BCB5自身所带的报表控件又显得缺少灵活性。所以,我想到了用TPrinter来打印报表。使用TPrinter必须要在程序头部加入#include <vclPrinters.hpp>
    由于对于不是链页打印纸来说,断页很困难,所以,我所需要设定每页打印的记录数。下面就是我自己编制的一个每页打印30条符合条件记录的代码,写给大家,以供参考。
try{
//定位打印头
  int x,y;
  x=170;
  y=150;
  Printer()->BeginDoc();//开始一个打印工作
//打印标头
  Printer()->Canvas->Font->Size=24;
  Printer()->Canvas->Font->Name="黑体";
  Printer()->Canvas->TextOut(x,y," -----服装请领记录-----");
  Printer()->Canvas->Font->Size=10;
  Printer()->Canvas->Font->Name="宋体";
  x=190;
  y=y+300;
//打印表格标题行
  Printer()->Canvas->TextOut(x,y," ◇"+Trim(Label12->Caption));
  y=y+80;
  Printer()->Canvas->TextOut(x,y,"┏━━━━━━━━━┳━━━━━━━━━━┳━━┳━━━━━┳━━━━━━━━━━━┳━━━━━━━┓");
  y=y+80;
  Printer()->Canvas->TextOut(x,y," 日 期 品 名 姓 名 部 门 职 务");
  Printer()->Canvas->TextOut(x+1820,y,"数量");
  Printer()->Canvas->TextOut(x,y,"┃");
  Printer()->Canvas->TextOut(x+830,y,"┃");
  Printer()->Canvas->TextOut(x+1742.1,y,"┃");
  Printer()->Canvas->TextOut(x+1991.2,y,"┃");
  Printer()->Canvas->TextOut(x+2491.73,y,"┃");
  Printer()->Canvas->TextOut(x+3487.61,y,"┃");
  Printer()->Canvas->TextOut(x+4151,y,"┃");
  ADOQuery1->First();
//换页记录计数器初始化
  int i=0;
//建立循环,如果不是最后一条记录则进行循环
while(!ADOQuery1->Eof)
{
//计数器加1
  i=i+1;
  y=y+80;
//打印第一条记录
  Printer()->Canvas->TextOut(x,y,"┣━━━━━━━━━╋━━━━━━━━━━╋━━╋━━━━━╋━━━━━━━━━━━╋━━━━━━━┫");
  y=y+80;
  Printer()->Canvas->TextOut(x,y,"┃"+ADOQuery1->FieldByName("请领日期")->AsString);
  Printer()->Canvas->TextOut(x+830,y,"┃"+ADOQuery1->FieldByName("品名")->AsString);
  Printer()->Canvas->TextOut(x+1742.1,y,"┃"+ADOQuery1->FieldByName("数量")->AsString);
  Printer()->Canvas->TextOut(x+1991.2,y,"┃"+ADOQuery1->FieldByName("请领人")->AsString);
  Printer()->Canvas->TextOut(x+2491.73,y,"┃"+ADOQuery1->FieldByName("部门")->AsString);
  Printer()->Canvas->TextOut(x+3487.61,y,"┃"+ADOQuery1->FieldByName("职务")->AsString);
  Printer()->Canvas->TextOut(x+4151,y,"┃");
  ADOQuery1->Next();
//测试是否该换页,每30条记录换一次页
  if(i%30==0)
  { 
//如果已经是最后一条记录,则退出循环
    if(ADOQuery1->Eof)
    {
      break;
     }
//如果不是最后一条记录
  else
  {
//如果该换页,则打印页尾和页码
    int p=Printer()->PageNumber;
    y=y+80;
    Printer()->Canvas->TextOut(x,y,"┗━━━━━━━━━┻━━━━━━━━━━┻━━┻━━━━━┻━━━━━━━━━━━┻━━━━━━━┛");
    Printer()->Canvas->TextOut(3800,y+200,"页码:"+IntToStr(p));
    Printer()->NewPage();
    Application->MessageBox("请更换纸张后按确定!","提示",MB_OK);
    x=170;
    y=150;
    Printer()->Canvas->Font->Size=24;
    Printer()->Canvas->Font->Name="黑体";
    Printer()->Canvas->TextOut(x,y," -----服装请领记录-----");
    Printer()->Canvas->Font->Size=10;
    Printer()->Canvas->Font->Name="宋体"; 
    x=190; 
    y=y+300; 
    Printer()->Canvas->TextOut(x,y," ◇"+Trim(Label12->Caption)); 
    y=y+80; 
    Printer()->Canvas->TextOut(x,y,"┏━━━━━━━━━┳━━━━━━━━━━┳━━┳━━━━━┳━━━━━━━━━━━┳━━━━━━━┓"); 
    y=y+80; 
    Printer()->Canvas->TextOut(x,y," 日 期 品 名 姓 名 部 门 职 务"); 
    Printer()->Canvas->TextOut(x+1820,y,"数量"); 
    Printer()->Canvas->TextOut(x,y,"┃"); 
    Printer()->Canvas->TextOut(x+830,y,"┃"); 
    Printer()->Canvas->TextOut(x+1742.1,y,"┃"); 
    Printer()->Canvas->TextOut(x+1991.2,y,"┃"); 
    Printer()->Canvas->TextOut(x+2491.73,y,"┃"); 
    Printer()->Canvas->TextOut(x+3487.61,y,"┃"); 
    Printer()->Canvas->TextOut(x+4151,y,"┃");
   }
  }
//换页结束
}
//打印完全部记录后,打印表格尾部
y=y+80;
Printer()->Canvas->TextOut(x,y,"┗━━━━━━━━━┻━━━━━━━━━━┻━━┻━━━━━┻━━━━━━━━━━━┻━━━━━━━┛");
//取得总页数
int pp=Printer()->PageNumber;
//在表格尾部打印页码
Printer()->Canvas->TextOut(3800,y+200,"页码:"+IntToStr(pp));
Printer()->EndDoc();//结束一个打印工作
ShowMessage("打印完成!谢谢使用!");
}
//异常处理
catch(...)

    ShowMessage("打印出错!请检查打印机是否连接好或者电源是否打开及其设置!");
}

                    欢迎光临黑兵网站http://www.ancn.net
                                                    E-mail:621150@0451.com QQ:621150

补充:软件开发 , Delphi ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,