unit ComplexReportForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  BaseForm, Db, ADODB, AnyPrint, AnyGrids, ComCtrls, Buttons, ExtCtrls;

type
  TfrmComplexReport = class(TfrmBase)
    ADOQuery1: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure AnyGridGetPageBreak(Sender: TObject; Row: Integer;
      var ToBreak: Boolean);
  private
    FStream:TStream;
    FTitleStream:TStream;
    FRows:integer;
    FCols:integer;
  protected
    procedure InitReport(Report:TAnyPrint);override;    
  public
    { Public declarations }
  end;

var
  frmComplexReport: TfrmComplexReport;

implementation

{$R *.DFM}

procedure TfrmComplexReport.FormCreate(Sender: TObject);
begin
  inherited;
  FStream:=TMemoryStream.Create;
  FTitleStream:=TMemoryStream.Create;
  ADOQuery1.ConnectionString:=Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sge1.mdb;Persist Security Info=False',
    [ExtractFilePath(Application.ExeName)]);
end;

procedure TfrmComplexReport.FormDestroy(Sender: TObject);
begin
  inherited;
  FStream.Destroy;
  FTitleStream.Free;
end;

procedure TfrmComplexReport.FormShow(Sender: TObject);
var
  Count:integer;
  BeginRow,BeginCol:integer;
  yy,mm,dd:Word;
  ADate:TDateTime;
  Week:Word;
  No:string;
  qjts,cqts,zcjb,xxrjb,jjrjbts:integer;
  Value:string;
begin
  inherited;
  FRows:=AnyGrid.Rows-1;
  FCols:=AnyGrid.Cols;
  AnyGrid.SaveCellsToStream(0,0,1,FCols,FTitleStream);
  AnyGrid.SaveCellsToStream(1,0,FRows,FCols,FStream);

  ADOQuery1.Close;
  ADOQuery1.SQL.Text:='select RB200604.[no] from RB200604 group by RB200604.[no]';
  ADOQuery1.Open;
  ADOQuery1.Last;
  Count:=ADOQuery1.RecordCount div 2;
  if (ADOQuery1.RecordCount mod 2)<>0 then
    Inc(Count);

  ADOQuery1.Close;
  ADOQuery1.SQL.Text:='select * from RB200604 order by RB200604.[no],RB200604.[kqrq]';
  ADOQuery1.Open;

  AnyGrid.BeginUpdate;
  try
    AnyGrid.Rows:=Count*FRows+1;
    AnyGrid.LoadCellsFromStream(FTitleStream,0,0,True);
    ADOQuery1.First;
    Count:=1;
    BeginRow:=1;
    BeginCol:=0;
    No:=Trim(ADOQuery1.FieldByName('no').AsString);
    AnyGrid.LoadCellsFromStream(FStream,BeginRow,0,True);
    qjts:=0;
    cqts:=0;
    zcjb:=0;
    xxrjb:=0;
    jjrjbts:=0;
    while not ADOQuery1.Eof do
    begin
      if No<>Trim(ADOQuery1.FieldByName('no').AsString) then
      begin
        No:=Trim(ADOQuery1.FieldByName('no').AsString);
        Inc(Count);
        //----------------------------------------------------------------------
        //填写小计
        AnyGrid.Cells(BeginRow+FRows-1,BeginCol+7).Text:=IntToStr(qjts); //请假天数
        AnyGrid.Cells(BeginRow+FRows-1,BeginCol+8).Text:=IntToStr(cqts); //出勤天数
        AnyGrid.Cells(BeginRow+FRows-1,BeginCol+9).Text:=IntToStr(zcjb); //正常加班
        AnyGrid.Cells(BeginRow+FRows-1,BeginCol+10).Text:=IntToStr(xxrjb); //周六加班
        AnyGrid.Cells(BeginRow+FRows-1,BeginCol+11).Text:=IntToStr(jjrjbts); //节加天数
        //----------------------------------------------------------------------
        if Count=3 then
        begin
          Count:=1;
          Inc(BeginRow,FRows);
          FStream.Position:=0;
          AnyGrid.LoadCellsFromStream(FStream,BeginRow,0,True);
        end;
        //----------------------------------------------------------------------
        //重新初始化小计
        qjts:=0;
        cqts:=0;
        zcjb:=0;
        xxrjb:=0;
        jjrjbts:=0;
        //----------------------------------------------------------------------
      end;
      if Count=1 then
      begin
        BeginCol:=0;
      end
      else
        BeginCol:=13;  //第偶数个职工的记录数据写在第14列开始的位置
      with ADOQuery1 do
      begin
        AnyGrid.Cells(BeginRow,BeginCol).Text:=Trim(FieldByName('no').AsString);   //职工编号
        AnyGrid.Cells(BeginRow,BeginCol+1).Text:=Trim(FieldByName('name').AsString);  //职工姓名
        ADate:=FieldByName('kqrq').AsDateTime;
        DecodeDate(ADate,yy,mm,dd);
        Week:=DayOfWeek(ADate)-1;
        if Week=0 then
          Week:=7;
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+3).Text:=IntToStr(Week);   //星期
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+4).Text:=Trim(FieldByName('sksj').AsString); //刷卡时间
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+5).Text:=Trim(FieldByName('ycyy').AsString); //异常原因
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+6).Text:=Trim(FieldByName('jzmc').AsString); //假种名称
        Value:=Trim(FieldByName('qjts').AsString);
        qjts:=qjts+StrToIntDef(Value,0);
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+7).Text:=Value; //请假天数
        Value:=Trim(FieldByName('cqts').AsString);
        cqts:=cqts+StrToIntDef(Value,0);
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+8).Text:=Value; //出勤天数
        Value:=Trim(FieldByName('zcjb').AsString);
        zcjb:=zcjb+StrToIntDef(Value,0);
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+9).Text:=Value; //正常加班
        Value:=Trim(FieldByName('xxrjb').AsString);
        xxrjb:=xxrjb+StrToIntDef(Value,0);
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+10).Text:=Value; //周六加班
        Value:=Trim(FieldByName('jjrjbts').AsString);
        jjrjbts:=jjrjbts+StrToIntDef(Value,0);
        AnyGrid.Cells(BeginRow+dd-1,BeginCol+11).Text:=Value; //节加天数
      end;
      ADOQuery1.Next;
    end;
    //--------------------------------------------------------------------------
    //填写小计
    AnyGrid.Cells(BeginRow+FRows-1,BeginCol+7).Text:=IntToStr(qjts); //请假天数
    AnyGrid.Cells(BeginRow+FRows-1,BeginCol+8).Text:=IntToStr(cqts); //出勤天数
    AnyGrid.Cells(BeginRow+FRows-1,BeginCol+9).Text:=IntToStr(zcjb); //正常加班
    AnyGrid.Cells(BeginRow+FRows-1,BeginCol+10).Text:=IntToStr(xxrjb); //周六加班
    AnyGrid.Cells(BeginRow+FRows-1,BeginCol+11).Text:=IntToStr(jjrjbts); //节加天数
    //--------------------------------------------------------------------------
  finally
    AnyGrid.EndUpdate;
  end;
end;

procedure TfrmComplexReport.InitReport(Report: TAnyPrint);
begin
  Report.TitleRows:=AnyGrid.FixRowCount;
  Report.PageFooter.RightText:='[PAGE#] of [TOTALPAGES] 页';//Display PageNo at RightBottom
end;

procedure TfrmComplexReport.AnyGridGetPageBreak(Sender: TObject;
  Row: Integer; var ToBreak: Boolean);
begin
  ToBreak:= (Row>1) and ((Row mod 32)=1); //从合计的下一行分页
end;

end.
