⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unmakereportdetail.pas

📁 本产品是开发LISTVIEW相关功能的可定制产品
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{**********************************************************************
                           通用报表打印定制类
名称:       报表打印列定制程序
目的:      按照用户要求定制报表详细打印列
功能:      按照用户要求定制报表详细打印列
作者:      邓普德
版权:      成都四方信息技术有限公司
使用限制:  最多打印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 + -