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

📄 rpttable.pas

📁 用delphi开发的电力调度操作票系统并带有access数据库
💻 PAS
📖 第 1 页 / 共 3 页
字号:
unit RptTable;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, QuickRpt, QRCtrls, ExtCtrls, StdCtrls,IniFiles,
  Common, ComCtrls, ToolWin, DB, ADODB, ActnList, StdActns, Menus,
  ActnMan, ActnCtrls, ImgList, DBCtrls, Grids, DBGrids, jpeg, Buttons;

  //-->表格控制参数类型
type
  TTBPara = record
    Top,Left,LinesH:Integer;
    Head,Band,Body,Summary,Foot:TStrings;
    Parent:TWinControl;
    FontStyle:TFont;
    Title:string;
  end;
  //<--Top,Left,LinesH分别表示表格左边,上边的坐标,表格行高
  //<--Parent父窗口
  //<--字体格式
  //Title表级标题
type
  TFrm_Table = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    QuickRep1: TQuickRep;
    ScrollBox1: TScrollBox;
    DetailBand1: TQRBand;
    ADOQuery: TADOQuery;
    QRLB_Title: TQRLabel;
    QRS_Title: TQRShape;
    PM_Cell: TPopupMenu;
    N1: TMenuItem;
    ActionList: TActionList;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    D1: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    EditCut1: TEditCut;
    EditCopy1: TEditCopy;
    EditPaste1: TEditPaste;
    EditSelectAll1: TEditSelectAll;
    EditUndo1: TEditUndo;
    EditDelete1: TEditDelete;
    N9: TMenuItem;
    ImageList1: TImageList;
    HeaderControl1: THeaderControl;
    Panel3: TPanel;
    LB_CZ: TListBox;
    Panel4: TPanel;
    Label1: TLabel;
    ED_Num: TEdit;
    DBGrid: TDBGrid;
    ADOTable: TADOTable;
    DataSource: TDataSource;
    QRImage1: TQRImage;
    BT_Select: TBitBtn;
    SaveDialog1: TSaveDialog;
    StatusBar: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure HeaderControl1SectionClick(HeaderControl: THeaderControl;
      Section: THeaderSection);
    procedure FormResize(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PM_CellPopup(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ED_NumChange(Sender: TObject);
    procedure ED_NumKeyPress(Sender: TObject; var Key: Char);
    procedure LB_CZKeyPress(Sender: TObject; var Key: Char);
    procedure LB_CZDblClick(Sender: TObject);
    procedure LB_CZClick(Sender: TObject);
    procedure DBGridDblClick(Sender: TObject);
    procedure DBGridKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure D1Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure DetailBand1AfterPrint(Sender: TQRCustomBand;
      BandPrinted: Boolean);


  private
    { Private declarations }

    ED_Cell:array of array of TQRRichText;
    Text_Cell:array of array of TEdit;
    content:TKcontent; //自定义记录型变量,用于记录操作内容等信息
    TableName:string;//表类型
    prefix,suffix:string; //前缀,后缀
    OrderUnit:string;

    procedure DrawTable(Para:TTBPara);//画表格由TQRRichText和TQRshape组成的二维表
    function ReadIni(TableName:string):TTBPara; // 读取表格布局信息
    function ReadInfo(TableName:string):Bool;//读取表的基本内容,包括居中信息等
    function ReadTableName:string; // 从INI文件中读取 TableName(票类型)

    procedure SetData; //从数据库中读出操作内容等基本信息
    procedure SaveData;//把在表格上的修改保存到Content中
    procedure AddEdit;  //创建与 TQRRichText对应的表格
    procedure GetText;//把 ED_Cell中的内容放到Text_Cell中
    procedure AddPage;//添加新页
    procedure SaveReport(content:TKcontent;FileName:string);
  public
    { Public declarations }
    HeadC,Bandc,Bodyc,SummaryC,FootC:integer;//记录表格各部分每一行的位置
    LinesC,PageIndex,PageC:integer;
    CurrentLine:integer;//当前行
    procedure BindData(Index:integer);// 对表格赋值,主要是操作内容

  end;

var
  Frm_Table: TFrm_Table;

implementation
uses
  Main,Print;
{$R *.dfm}
procedure TFrm_Table.SaveReport(content:TKcontent;FileName:string);
var
  ReportFile:TextFile;
  i:integer;
begin
  //
  AssignFile(ReportFile,FileName);
  Rewrite(ReportFile);
  WriteLn(ReportFile,TableName);
  for i:=0 to content.Body.Count-1 do
  begin
    WriteLn(ReportFile,trim(content.Body[i]));
  end;
  if (trim(OrderUnit)<>'') then WriteLn(ReportFile,OrderUnit);
  WriteLn(ReportFile,content.Title);
  closeFile(ReportFile);
end;

procedure TFrm_Table.AddPage;
begin
  if PageIndex=PageC-1 then
    if content.Body.Count mod LinesC=1 then Inc(PageIndex);
end;

procedure TFrm_Table.GetText;
var
  i,j:integer;
begin
   for  i:=low(Text_Cell)  to High(Text_Cell)   do
   begin
     for j := low(Text_Cell[i]) to High(Text_Cell[i])  do
      begin
        ED_Cell[i][j].Lines.Text:=Text_Cell[i][j].Text;
        Application.ProcessMessages;
      end;
   end;
end;

procedure TFrm_Table.AddEdit;
var
  i,j:integer;
begin
  SetLength(Text_Cell,High(ED_Cell)+1);
  for  i:=low(Text_Cell)  to High(Text_Cell)   do
   begin
      SetLength(Text_Cell[i],High(ED_Cell[i])+1);
      for j := low(Text_Cell[i]) to High(Text_Cell[i])  do
      begin
        Text_Cell[i][j]:=TEdit.Create(self);
        Text_Cell[i][j].Parent:=Self.QuickRep1;

        Text_Cell[i][j].Left:=ED_Cell[i][j].Left+1;
        Text_Cell[i][j].Top:=ED_Cell[i][j].Top;
        Text_Cell[i][j].Width:=ED_Cell[i][j].Width-3;
        Text_Cell[i][j].Height:=ED_Cell[i][j].Height-8;

        Text_Cell[i][j].BringToFront;
        Text_Cell[i][j].Ctl3D:=False;
        Text_Cell[i][j].BorderStyle:=bsNone;
        Text_Cell[i][j].Font:=ED_Cell[i][j].Font;
        Text_Cell[i][j].PopupMenu:=PM_Cell;
        Text_Cell[i][j].OnClick:=PM_Cell.OnPopup;
        if j=0 then //使第一列文本居中
           SetWindowLong(Text_Cell[i][j].Handle,   GWL_STYLE,   GetWindowLong(Text_Cell[i][j].Handle,   GWL_STYLE)   or   ES_CENTER);

        Application.ProcessMessages;
      end;
   end;
 
end;

procedure TFrm_Table.SaveData;//把在表格上的修改保存到Content中
var
  i:integer;
begin
  for i:=0 to LinesC-1 do
  begin
    if (Low(Text_Cell)<=BandC+i+1)and(BandC+i+1<=High(Text_Cell))
        and(High(Text_Cell[BandC+i+1])>1)  then
    begin

      if i+(PageIndex)*LinesC<Content.Body.Count then
        Content.Body[i+PageIndex*LinesC]:=Text_Cell[BandC+i+1][1].Text
      else
      if trim(Text_Cell[BandC+i+1][1].Text)<>'' then
        Content.Body.Add(Text_Cell[BandC+i+1][1].Text);
    end;
      
  end;
end;

procedure TFrm_Table.BindData(Index:integer);  //对表格赋值,主要是操作内容
var
  i:integer;
begin

  //计算总页数
  if content.Body.Count mod LinesC=0 then
    PageC:=content.Body.Count div LinesC
  else
    PageC:=content.Body.Count div LinesC+1; //总页数]

  StatusBar.Panels[0].Text:='共:'+IntToStr(PageC)+'页';
  if content.Body.Count=0 then
    StatusBar.Panels[1].Text:='当前第:0页'
  else
    StatusBar.Panels[1].Text:='当前第:'+IntToStr(PageIndex+1)+'页';

  Text_Cell[Bandc-1][1].Text:=' '+content.Title;
  for i:=1  to LinesC  do
  begin
    Text_Cell[Bandc+i][0].Clear;
    Text_Cell[Bandc+i][1].Clear;
    if i+Index*LinesC<Content.Body.Count+1 then
    begin
      Text_Cell[Bandc+i][0].Text:=inttostr(i+Index*LinesC);
      Text_Cell[Bandc+i][1].Text:=' '+trim(Content.Body[i+Index*LinesC-1]);
      Application.ProcessMessages;
    end;
  end;
  //添加用户名到签发人
  if (TableName='ZH.ini')or(TableName='AC1.ini')or(TableName='AC2.ini') then
  begin
    ED_Cell[BodyC][1].Alignment:=taCenter;
    SetWindowLong(Text_Cell[BodyC][1].Handle,   GWL_STYLE,   GetWindowLong(Text_Cell[BodyC][1].Handle,   GWL_STYLE)   or   ES_CENTER);
    Text_Cell[BodyC][1].Text:=' '+UserNum;
  end;

  ReadInfo(TableName); //读出表格基本信息
  GetText;//把 ED_Cell中的内容放到Text_Cell中
  
end;

procedure TFrm_Table.SetData; //从数据库中读出操作内容等基本信息
var
  S:string;
begin
  content.Body:=TStringList.Create;
  ADOQuery.Connection:=Frm_Com.ADOConnection;
  ADOQuery.SQL.Text:='select * from CZNR  order by 序号';
  ADOQuery.Open;
  ADOQuery.First;
  while not ADOQuery.Eof do
  begin
    s:=ADOQuery.FieldByName('操作内容').AsString;
    Content.Body.Add(S);
    ADOQuery.Next;
  end;
  ADOQuery.Close;
  if content.Body.Count>0 then
  begin
    content.Title:=Content.Body[Content.Body.count-1]; //读取任务标题
    Content.Body.Delete(Content.Body.count-1);  //从body中删除
  end;
  if content.Body.Count>0 then
  begin
    OrderUnit:=Content.Body[Content.Body.count-1]; //读取发令单位
    Content.Body.Delete(Content.Body.count-1);  //从body中删除
  end;
  

  LinesC:=BodyC-BandC-1;             //行数
  PageIndex:=0;                 //当前页数
  if content.Body.Count mod LinesC=0 then
    PageC:=content.Body.Count div LinesC
  else
    PageC:=content.Body.Count div LinesC+1; //总页数

  BindData(0);
end;

function TFrm_Table.ReadTableName:string;  //从INI文件中读取 TableName(票类型)
var
  IniFile:TIniFile;
  FileName:string;
begin
   //从INI文件中读取 TableName(票类型)
  
  FileName:=ExtractFilePath(Application.ExeName)+'Config\TableName.ini'; //获取INI文件路径
  if FileExists(FileName) then
  begin
    IniFile:=TIniFile.Create(FileName);
    try
      Result:=IniFile.ReadString('Table','Name','');
    finally
      IniFile.Free;
    end;

  end
  else
  begin
    result:='';
  end;
end;

function TFrm_Table.ReadInfo(TableName:string):Bool;  //读出表格布局及固定内容等信息
var
  i,j:integer;
  F:TextFile;
  FileName,FileDir:string;
  S,x,y:string;
begin
  if TableName='ZH.ini' then Text_Cell[0][3].Text:=' '+OrderUnit; //为综合令票添加发令单位

  i:=0;j:=0;
  FileDir:=ExtractFilePath(Application.ExeName)+'Config\';

  FileName:=FileDir+'CT'+TableName;

  //-->读取要居中的单元格
  if FileExists(FileName) then
  begin
    AssignFile(F,FileName);
    Reset(F);
    try
      while not Eof(F) do
      begin
        ReadLn(F,S);
        if trim(s)<>'' then
        begin
          x:=copy(s,0,pos(',',s)-1);
          if trim(x)<>'' then
            i:=strtoint(x);
          y:=copy(s,pos(',',s)+1,length(s));
          if trim(y)<>'' then
            j:=strtoint(y);
          if i<=High(ED_Cell) then
            if j<=High(ED_Cell[i]) then
            begin
               ED_Cell[i][j].Alignment:=taCenter;
               SetWindowLong(Text_Cell[i][j].Handle,   GWL_STYLE,   GetWindowLong(Text_Cell[i][j].Handle,   GWL_STYLE)   or   ES_CENTER);
            end;
        end;
      end;
     finally
      CloseFile(F);
     end;

  end
  else
  begin
    showmessage('出错了,配置文件不存在!');
    Result:=False;
    exit;
  end;

  
  FileName:=FileDir+'Text'+TableName;
  //读取初始单元格信息
  if FileExists(FileName) then
  begin
    AssignFile(F,FileName);
    Reset(F);
    try
      while not Eof(F) do
      begin
        ReadLn(F,S);
        if trim(s)<>'' then
        begin
          x:=copy(s,0,pos(',',s)-1);
          if trim(x)<>'' then
            i:=strtoint(x);
          s:=copy(s,pos(',',s)+1,length(s));
          y:=copy(s,0,pos(',',s)-1);
          if trim(y)<>'' then
            j:=strtoint(y);
          s:=copy(s,pos(',',s)+1,length(s));
        if i<=High(Text_Cell) then
          if j<=High(Text_Cell[i]) then
             begin
               Text_Cell[i][j].Text:=S;
               //Text_Cell[i][j].Enabled:=False;
             end;
        end;
      end;


    finally
      CloseFile(F);
    end;
    Result:=True;
  end
  else
  begin
    showmessage('出错了,配置文件不存在!');
    Result:=False;
  end;

  


end;

procedure TFrm_Table.DrawTable(Para:TTBPara);
var
  i,j:integer;
  QRS:TQRShape;
  RowC:integer;
  
begin
  //-->计算各部分行数
  RowC:=strtoint(Para.Head[0]);
  HeadC:=Rowc;
  RowC:=strtoint(Para.Band[0]);
  BandC:=RowC+HeadC;
  RowC:=strtoint(Para.Body[0]);
  BodyC:=RowC+BandC;
  RowC:=strtoint(Para.Summary[0]);
  SummaryC:=RowC+BodyC;
  RowC:=strtoint(Para.Foot[0]);
  FootC:=RowC+SummaryC;
   //画背景纸张
  QRS:=TQRShape.Create(self);
  QRS.Parent:=Para.Parent;
  QRS.Left:=Para.Parent.Left;
  QRS.Top:=Para.Parent.Top;
  QRS.Width:=Para.Parent.Width;
  QRS.Height:=Para.Parent.Height;
  QRS.SendToBack;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -