1. 程式人生 > >DELPHI製作複雜報表例項,特別推薦!

DELPHI製作複雜報表例項,特別推薦!

 我近段時間一直在做各種各樣的報表,特別是那些複雜報表,如下圖所示:

像這類報表若說用第三方控制元件,說實在,你還得重做一次表,你做的又不一定符合人家原來做的要求,若用用友軟體,那得交一筆不小的數目的錢哦!可我們公司一直要求以最小的投入獲取最大的回報,所以是不可能的,後面想想,他們是用EXCEL做成的表給我的,我為什麼不可以直接呼叫它們的表呢,我只要把這些表做成模板再與資料庫相連,便可實現了,後來就按這方案去進行了,結果當然是搞定了,現在公佈出來與大家共享,當然,對與高手而言也許不是怎麼高的水平,但我相信會對部分人有用的,像這類報表做的思想就是:!一.定義各類資料的行列;二,連線模板;三,按要求進行相關的資料提取和相關的運算,對於資料運算一定要儘量想到EXCE的強大的運算功能,只要它能幫解決的就不要程式裡面編寫,否則執行起來就不是那麼快了,相關程式碼如下:

procedure Tdp_bb.dp_okClick(Sender: TObject);
var
  cbw,zbw,wbw,rjw,ph,ExeRootPath:string;
  i,j,k:integer;
  t1,t2:Ttime;
begin
  rjw:=trim(datetostr(dp_dtp.Date));
  cbw := trim(dp_cbbc.Text);
  zbw := trim(dp_cbbz.Text);
  wbw := trim(dp_cbbw.Text);
  t1:=0;
  t2:=0;
  if (cbw<>'') and (zbw<>'') and (wbw<>'') then
  begin
    try
      ChDir(ExtractFilePath(Application.ExeName));
      ChDir('..');
      ExeRootPath := GetCurrentDir;
      dp_exapt.Connect;
      dp_exapt.Visible[0]:=false;
      dp_exapt.Workbooks.Add(ExeRootPath+'/config/sjcs.xls',0);
      dp_exwb.ConnectTo(dp_exapt.Workbooks[1]);
      dp_exws.ConnectTo(dp_exwb.Sheets[1] as _worksheet);
      j:=6;//起始行初始化
      with dp_form.dp_adoq1 do
      begin
        close;
        sql.Clear;
        sql.Add('select GWDY_SJPH,GWDY_ZTP from TB_GWDY order by GWDY_XH asc');
        open;
        while not eof do
        begin
          ph:=fieldbyname('GWDY_SJPH').AsString;
          i:=fieldbyname('GWDY_ZTP').AsInteger;
          if i=6 then
          {時間型別}
          begin
          {早班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPTM from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+cbw+''' and YS_DATE='''+rjw+''' order by YS_XH asc');
              open;
              for k:=6 to RecordCount+5 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPTM').AsString);
                next;
              end;
            end;
          {中班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPTM from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+zbw+''' and YS_DATE='''+rjw+''' order by YS_XH asc');
              open;
              for k:=11 to RecordCount+10 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPTM').AsString);
                next;
              end;
            end;
          {晚班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPTM from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+wbw+''' and YS_DATE='''+rjw+''' order by YS_XH asc');
              open;
              for k:=16 to RecordCount+15 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPTM').AsString);
                next;
              end;
            end;
            {月平均}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPTM from TB_YS where YS_SJPH='''+ph+''' and month(YS_DATE)='''+formatdatetime('mm',dp_dtp.Date)+''' order by YS_XH asc');
              open;
              while not eof do
              begin
                t1:=t1+strtotime(trim(fieldbyname('YS_YPTM').AsString));
                next;
              end;
              dp_exws.Cells.Item[j,22]:=timetostr(t1);
              t1:=0;
            end;
            {榨季平均}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPTM from TB_YS where YS_SJPH='''+ph+''' order by YS_XH asc');
              open;
              while not eof do
              begin
                t2:=t2+strtotime(trim(fieldbyname('YS_YPTM').AsString));
                next;
              end;
              dp_exws.Cells.Item[j,23]:=timetostr(t2);
              t2:=0;
            end;
          end
          else
        {非時間型別}
          begin
        {早班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPS from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+cbw+''' and YS_DATE='''+rjw+''' and YS_YPTM in(''2:30'',''4:30'',''6:30'',''8:30'','''') order by YS_XH asc');
              open;
              for k:=6 to RecordCount+5 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPS').AsString);
                next;
              end;
            end;
          {中班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPS from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+zbw+''' and YS_DATE='''+rjw+''' and YS_YPTM in(''10:30'',''12:30'',''14:30'',''16:30'','''') order by YS_XH asc');
              open;
              for k:=11 to RecordCount+10 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPS').AsString);
                next;
              end;
            end;
          {晚班資料位置}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select YS_YPS from TB_YS where YS_SJPH='''+ph+''' and YS_BB='''+wbw+''' and YS_DATE='''+rjw+''' and YS_YPTM in(''18:30'',''20:30'',''22:30'',''00:30'','''') order by YS_XH asc');
              open;
              for k:=16 to RecordCount+15 do
              begin
                dp_exws.Cells.Item[j,k]:=trim(fieldbyname('YS_YPS').AsString);
                next;
              end;
            end;
            {月平均}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select avg(YS_YPS) as A from TB_YS where  YS_SJPH='''+ph+''' and month(YS_DATE)='''+formatdatetime('mm',dp_dtp.Date)+''' group by YS_SJPH');
              open;
              if not eof then
              begin
                dp_exws.Cells.Item[j,22]:=trim(fieldbyname('A').AsString);
              end;
            end;
            {榨季平均}
            with dp_form.dp_adoq2 do
            begin
              close;
              sql.Clear;
              sql.Add('select avg(YS_YPS) as B from TB_YS where YS_SJPH='''+ph+''' group by YS_SJPH');
              open;
              if not eof then
              begin
                dp_exws.Cells.Item[j,23]:=trim(fieldbyname('B').AsString);
              end;
            end;
          end;
          j:=j+1;
          next;
        end;
      end;
      dp_exws.Cells.Item[2,1] := rjw;
      dp_exws.Cells.Item[4,6] := cbw;
      dp_exws.Cells.Item[4,11]:= zbw;
      dp_exws.Cells.Item[4,16]:= wbw;
      bb_tm.Enabled:=true;
    except
      showmessage('模板路徑出錯或者是您還沒有安裝OFFICE辦公軟體');
      close;
    end;
  end;
end;

以上希望對學DELPHI的人有幫助!只要我擁有的知識一定與大家共享!