程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 如何用Tprinter設計BCB操作數據庫程序中的報表

如何用Tprinter設計BCB操作數據庫程序中的報表

編輯:Delphi
經常用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("打印出錯!請檢查打印機是否連接好或者電源是否打開及其設置!");
  }
  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved