?? unmakereportdetail.pas
字號:
{**********************************************************************
通用報表打印定制類
名稱: 報表打印列定制程序
目的: 按照用戶要求定制報表詳細打印列
功能: 按照用戶要求定制報表詳細打印列
作者: 鄧普德
版權: 成都四方信息技術有限公司
使用限制: 最多打印10列,且列名來自于指定數據庫
定義時間: 2006-08-02
修改時間: 2006-08-06
**********************************************************************}
unit UnMakeReportDetail;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,QuickRpt,DBClient,Grids,
ComCtrls, StdCtrls, ExtCtrls, ToolWin, Qrctrls,DB,DBTables,Printers,QRPrntr,unDetailReport;
{Self}
type
//為了定制報表,需要用戶在窗體初始化時創建一個TListViewColumn型樹組并填充如下內容
{統計查詢幾選擇打印字段屬性定義}
//該記錄的值可以作為歷史保存到數據庫中,用戶可以選擇歷史或默認打印
TMListData = array[0..10] of string[30];//最多可以打印10列,每列最多30個字符
PMListData = ^TMListData;
TListViewColumn = record
PrintCaption: string[14]; //報表實際打印列的標題
Caption: string[14]; //SOURCELISTVIES列顯示的標題
ColumnName: string[20]; //數據列名稱
Width: Integer; //數據列最大寬度
bSelect: Boolean; //True: 表示該列被選中打印;False: 表示該列沒有被選中;每次選擇修改后需要維護
PrintTab:Integer; //數據列在報表中的打印次序;每次選擇修改后需要維護
end;
//報表打印定制類定義
TMakeReportDetail = class(TObject)
private
PrintColumnNum,PrintModal:Integer;//打印列的列數及紙張如何打印(橫打還是豎打);選項有:(poPortrait, poLandscape)
ReportForm:TForm; //SourceListView所在的窗體名稱;
ReportFormName,ReportName:string;//SourceListView所在的窗體名稱;報表名稱
SourceListView:TListView; //待打報表中的LISTVIEW
ReportDataSet:TClientDataSet; //用于從LISTVIEW取出打印數據
DatabaseName:string; //外部系統訪問數據庫使用的數據庫名
SessionName:string; //外部系統訪問數據庫使用的會話名
FrmUniversalReport: TFrmReport; //本類所引用的報表窗體
PrintColum:array of integer; //保存打印列在可顯示數組中的位置
TotalPrintColumNumber:integer; //總共需要打印列數
ColumnDefine:Array[0..9] of TListViewColumn; //打印列信息定義數組
constructor Create;
destructor Destroy;
function GetDataSetFromListview: TClientDataSet;
function GetDataSetFromList(Rlist:TList): TClientDataSet;
procedure GetColumnDefineFromTable;
procedure MakeDetailBand; //打印報表中的明細
procedure MakeTotalBand(sgNormal:TstringGrid);
public
//僅打印報表中的明細
//DoOnlyDetailPreview針對于未進行顯示定制的窗體LISTVIEW的定制打印
function DoOnlyDetailPreview(fReportFormName:TForm;lSourceListView:TListView;
sReportName,sDataBaseName,sSessionName,ReportFormTitle,UnitCaption,ReportCaption,
sStaffName:string;PrintDate:TDate;PaperSize:TQRPaperSize;MemoData:TMemo):Integer;
//DoOnlyDetailPreviewNew針對于已經進行過顯示定制的窗體LISTVIEW的定制打印——該方式下的速度更快
function DoOnlyDetailPreviewNew(fReportFormName:TForm;Rlist:TList;sReportName,
sDataBaseName,sSessionName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;
PrintDate:TDate;PaperSize:TQRPaperSize;MemoData:TMemo):Integer;
//僅打印報表中的匯總數據
function DoOnlyTotalPreview(fReportFormName:TForm;sReportName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;PrintDate:TDate;PaperSize:TQRPaperSize;sStringGrid:TstringGrid):Integer;
//打印報表中的明細及匯總數據
function DoAllPreview(fReportFormName:TForm;lSourceListView:TListView;sReportName,sDataBaseName,sSessionName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;PrintDate:TDate;PaperSize:TQRPaperSize;MemoData:TMemo;sStringGrid:TStringGrid):Integer;
function DoAllPreviewNew(fReportFormName:TForm;Rlist:TList;sReportName,sDataBaseName,sSessionName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;PrintDate:TDate;PaperSize:TQRPaperSize;MemoData:TMemo;sStringGrid:TStringGrid):Integer;
end;
var
MakeReportDetail: TMakeReportDetail;
implementation
uses unCDM;
//{$R *.DFM}
constructor TMakeReportDetail.Create;
begin
inherited;
TotalPrintColumNumber:=0;//Ini
End;
destructor TMakeReportDetail.Destroy;
begin
inherited;
End;
//取得數據表中定義的報表打印列信息
procedure TMakeReportDetail.GetColumnDefineFromTable;
var qQryDB:TQuery;
i:integer;
str:string;
rListViewColumn:TListViewColumn;
begin
str:='SELECT CAPTION,INARYSERIALID,COLUMNNAME,'+
' WIDTH,PRINTTYPE,PRINTCAPTION '+
' FROM SFRJPRINTDETAIL A,SFRJTABLEDETAIL B,SFRJREPORTNAME C '+
' WHERE A.REPORTID=C.REPORTID '+
' AND A.TABLEDETAILID=B.TABLEDETAILID '+
' AND C.FORMNAME ='''+ReportFormName+''' AND REPORTNAME='''+ReportName+
''' ORDER BY PRINTTAB ASC';
try
//創建數據表,并為其指定數據庫名稱及會話名稱
qQryDB:=TQuery.Create(nil);
if DataBaseName<>'' then
qQryDB.DatabaseName:=DataBaseName
else
qQryDB.DatabaseName:=CDM.dbData.DatabaseName;
if SessionName<>'' then
qQryDB.SessionName:=SessionName
else
qQryDB.SessionName:=CDM.dbData.SessionName;
qQryDB.close;
qQryDB.sql.Clear;
qQryDB.sql.Add(Str);
qQryDB.Open;
qQryDB.First;
PrintModal:=qQryDB.FieldByName('PRINTTYPE').AsInteger;
TotalPrintColumNumber:=qQryDB.RecordCount;//取得查詢到的記錄數
setlength(PrintColum,TotalPrintColumNumber);
i:=0;
while ((not qQryDB.eof) and (i<10)) do//最多打印10列
begin
PrintColum[i]:=qQryDB.FieldByName('INARYSERIALID').AsInteger;
rListViewColumn.PrintCaption:=qQryDB.FieldByName('PRINTCAPTION').AsString;
rListViewColumn.Caption:=qQryDB.FieldByName('CAPTION').AsString;
rListViewColumn.ColumnName:=qQryDB.FieldByName('COLUMNNAME').AsString;
rListViewColumn.Width:=qQryDB.FieldByName('WIDTH').AsInteger;
rListViewColumn.bSelect:=True;
rListViewColumn.PrintTab:=i;
ColumnDefine[i]:=rListViewColumn;
i:=i+1;
qQryDB.Next;
end;
PrintColumnNum:=i;
qQryDB.Close;
qQryDB.Free;
qQryDB:=nil;
except//異常后的處理
ShowMessage(' 取打印列信息出錯!');
qQryDB.Close;
qQryDB.Free;
qQryDB:=nil;
PrintColumnNum:=0;
Exit;
end;
end;
//對外調用接口
//輸入參數:
//lSourceListView:為查詢到需要定制打印輸出的數據源;
//fReportFormName:lSourceListView所在的窗體名稱;
//ReportFormTitle:需要顯示的報表窗體名稱;
//UnitCaption: 報表副標題,通常為擁有報表單位的名稱;
//ReportCaption: 報表主標題;
//sDataBaseName: 外部系統訪問數據庫使用的數據庫名
//sSessionName: 外部系統訪問數據庫使用的會話名
//sStaffName: 制表人
//sMemoData: 備注內容;TMemo
//PrintDate: 打印報表的日期;如:2003-11-21
//PaperSize: 打印報表的紙張類型;選項有:(Default, Letter, LetterSmall, Tabloid, Ledger, Legal,
// Statement, Executive, A3, A4, A4Small, A5, B4, B5, Folio,
// Quarto, qr10X14, qr11X17, Note, Env9, Env10, Env11, Env12,
// Env14, CSheet, DSheet, ESheet, Custom)
//PrintModal: 紙張如何打印(橫打還是豎打);選項有:(poPortrait, poLandscape)
function TMakeReportDetail.DoOnlyDetailPreview(fReportFormName:TForm;lSourceListView:TListView;
sReportName,sDataBaseName,sSessionName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;
PrintDate:TDate;PaperSize:TQRPaperSize;MemoData:TMemo):Integer;
begin
//將傳遞過來的參數初始化
result:=0;
PrintColumnNum:=0;
ReportForm:=fReportFormName;
ReportFormName:=ReportForm.Name;
ReportName:=sReportName;
DataBaseName:=sDataBaseName;
SessionName:=sSessionName;
SourceListView:=lSourceListView;
GetColumnDefineFromTable; //按照打印列定義從LISTVIEW中取出打印數據
if(PrintColumnNum=0)then exit; //打印列數=0,表示不用打印
if SourceListView.Items.Count <= 0 then exit; //LISTVIEW中無數據,表示不用打印
ReportDataSet :=GetDataSetFromListview; //從SOURCELISTVIEW中取各行數據到ReportDataSet中
try
FrmUniversalReport := TFrmReport.Create(nil);
except//創建打印窗體異常后的處理
ReportDataSet.Close;
ReportDataSet.Free;
ReportDataSet:=nil;
Showmessage('請檢查打印機是否有效且可用!');
Exit;
end;
with FrmUniversalReport do
begin
Caption:=ReportFormTitle;
FrmUnitName1.Caption:=UnitCaption;
FrmUnitName2.Caption:=UnitCaption;
FrmReportCaption.Caption:=ReportCaption;
FrmPrintDate1.Caption:=Datetostr(PrintDate);
FrmPrintDate2.Caption:=FrmPrintDate1.Caption;
QRLabel11.Caption:='制表人:'+sStaffName;
PrintReport.Page.PaperSize:=PaperSize;/////////////確定打印方式
If PrintModal=0 then
PrintReport.Page.Orientation:=poPortrait///////////
else
PrintReport.Page.Orientation:=poLandscape;///////////
FrmReportCaption.Left:=((ChildBand1.Width-FrmReportCaption.Width) div 2);//確定標題顯示位置
FrmPrintDate1.Left:=ChildBand1.Width+ChildBand1.Left-160; //確定日期顯示位置
FrmPrintDate2.Left:=ChildBand1.Width+ChildBand1.Left-150; //確定日期顯示位置
FrmMemo.Left:=ChildBand1.Width+ChildBand1.Left-500; //確定備注信息顯示位置
QRSysData2.Left:=(ChildBand1.Width div 2)+ChildBand1.Left-70;
QRLabel11.Left:=ChildBand1.Width+ChildBand1.Left-250;
if MemoData<>nil then
FrmMemo.lines:=MemoData.lines; //顯示備注信息
PrintReport.Bands.HasSummary := False;
MakeDetailBand; //調用創建明細信息
if printer.Printers.Count <> 0 then
begin
FrmUniversalReport.PrintReport.PreviewModal;
end
else
Application.MessageBox('沒有缺省打印機!,請添加打印機后重試。','系統提示',MB_ok);
ReportDataSet.Close;
ReportDataSet.Free;
ReportDataSet:=nil;
end;
FrmUniversalReport.Close;
FrmUniversalReport.Free;
FrmUniversalReport:=nil;
result:=999;
end;
//對外調用接口
//輸入參數:
//fReportFormName:lSourceListView所在的窗體名稱;
//ReportFormTitle:需要顯示的報表窗體名稱;
//UnitCaption: 報表副標題,通常為擁有報表單位的名稱;
//ReportCaption: 報表主標題;
//sStaffName: 制表人
//PrintDate: 打印報表的日期;如:2003-11-21
//PaperSize: 打印報表的紙張類型;選項有:(Default, Letter, LetterSmall, Tabloid, Ledger, Legal,
// Statement, Executive, A3, A4, A4Small, A5, B4, B5, Folio,
// Quarto, qr10X14, qr11X17, Note, Env9, Env10, Env11, Env12,
// Env14, CSheet, DSheet, ESheet, Custom)
//PrintModal: 紙張如何打印(橫打還是豎打);選項有:(poPortrait, poLandscape)
//sStringGrid: 為查詢到需要打印輸出的數據源;
function TMakeReportDetail.DoOnlyTotalPreview(fReportFormName:TForm;sReportName,ReportFormTitle,UnitCaption,ReportCaption,sStaffName:string;PrintDate:TDate;PaperSize:TQRPaperSize;sStringGrid:TStringGrid):Integer;
begin
result:=0;
PrintColumnNum:=0;
PrintModal:=3;
ReportForm:=fReportFormName;
ReportFormName:=ReportForm.Name;
ReportName:=sReportName;
if sStringGrid.RowCount<2 then exit;//無打印內容不顯示
FrmUniversalReport := TFrmReport.Create(nil);
try
FrmUniversalReport := TFrmReport.Create(nil);
except
Showmessage('請檢查打印機是否有效且可用!');
Exit;
end;
with FrmUniversalReport do
begin
Caption:=ReportFormTitle;
FrmUnitName1.Caption:=UnitCaption;
FrmReportCaption.Caption:='';
FrmReportCaption2.Caption:=ReportCaption;
FrmPrintDate1.Caption:=Datetostr(PrintDate);
QRLabel11.Caption:='制表人:'+sStaffName;
PrintReport.Page.PaperSize:=PaperSize;/////////////打印紙張及模式的設置
FrmReportCaption2.Left:=((ChildBand1.Width-FrmReportCaption.Width) div 2);
FrmPrintDate1.Left:=ChildBand1.Width+ChildBand1.Left-160;
QRShape1.Width:=arbStatics.Width-24;
QRShape1.Left:=12;
PrintReport.Bands.HasColumnHeader := False;
PrintReport.Bands.HasDetail := False;
MakeTotalBand(sStringGrid);
If PrintModal=0 then
PrintReport.Page.Orientation:=poPortrait
else
PrintReport.Page.Orientation:=poLandscape;///////////
if printer.Printers.Count <> 0 then
PrintReport.PreviewModal
else
Application.MessageBox('沒有缺省打印機!,請添加打印機后重試。','系統提示',MB_ok);
end;
FrmUniversalReport.Close;
FrmUniversalReport.Free;
FrmUniversalReport:=nil;
result:=999;
end;
//對外調用接口
//輸入參數:
//lSourceListView:為查詢到需要定制打印輸出的數據源;
//fReportFormName:lSourceListView所在的窗體名稱;
//ReportFormTitle:需要顯示的報表窗體名稱;
//UnitCaption: 報表副標題,通常為擁有報表單位的名稱;
//ReportCaption: 報表主標題;
//sDataBaseName: 外部系統訪問數據庫使用的數據庫名
//sSessionName: 外部系統訪問數據庫使用的會話名
//sStaffName: 制表人
//sMemoData: 備注內容;TMemo
//PrintDate: 打印報表的日期;如:2003-11-21
//PaperSize: 打印報表的紙張類型;選項有:(Default, Letter, LetterSmall, Tabloid, Ledger, Legal,
// Statement, Executive, A3, A4, A4Small, A5, B4, B5, Folio,
// Quarto, qr10X14, qr11X17, Note, Env9, Env10, Env11, Env12,
// Env14, CSheet, DSheet, ESheet, Custom)
//PrintModal: 紙張如何打印(橫打還是豎打);選項有:(poPortrait, poLandscape)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -