www.pudn.com > PrintServer.rar > ReportOfPrintRec.pas, change:2006-02-23,size:5787b


unit ReportOfPrintRec; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, DB, VirtualTable, MemDS, DBAccess, MSAccess, Buttons, 
  Grids, DBGridEh, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit, 
  cxDBLookupComboBox, ComCtrls, cxControls, cxContainer, cxEdit, 
  cxTextEdit, cxMaskEdit, cxCalendar, ExtCtrls, OleCtnrs, comobj, Excel2000, OleServer; 
 
type 
  TReportOfPrintRecForm = class(TForm) 
    GroupBox1: TGroupBox; 
    Panel1: TPanel; 
    StatusBar1: TStatusBar; 
    Label1: TLabel; 
    Label2: TLabel; 
    cxDateEdit1: TcxDateEdit; 
    cxDateEdit2: TcxDateEdit; 
    TempQuery: TMSQuery; 
    DataSource1: TDataSource; 
    DataSource2: TDataSource; 
    InfoQuery: TMSQuery; 
    BitBtn2: TBitBtn; 
    BitBtn5: TBitBtn; 
    BitBtn4: TBitBtn; 
    DBGridEh1: TDBGridEh; 
    ComboBox1: TComboBox; 
    Label3: TLabel; 
    cxLookupComboBox1: TcxLookupComboBox; 
    Label4: TLabel; 
    procedure Combobox1Exit(Sender: TObject); 
    procedure BitBtn4Click(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure FormCreate(Sender: TObject); 
    procedure cxDateEdit1Exit(Sender: TObject); 
    procedure cxDateEdit2Exit(Sender: TObject); 
    procedure BitBtn5Click(Sender: TObject); 
    procedure BitBtn2Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  ReportOfPrintRecForm: TReportOfPrintRecForm; 
 
implementation 
 
{$R *.dfm} 
 
procedure TReportOfPrintRecForm.Combobox1Exit(Sender: TObject); 
var 
  SQL_Str: string; 
begin 
  if (ComboBox1.Text <> '') then 
  begin 
    if (ComboBox1.Text = '姓名') then 
    begin 
      SQL_Str := 'Select * From 员工信息表 Order By 员工姓名'; 
      TempQuery.Active := False; 
      TempQuery.SQL.Clear; 
      TempQuery.SQL.Add(SQL_Str); 
      TempQuery.Open; 
      cxLookupComboBox1.Properties.KeyFieldNames := '员工姓名'; 
      cxLookupComboBox1.Properties.ListFieldNames := '员工姓名'; 
    end 
    else 
      if (ComboBox1.Text = '部门') then 
    begin 
      SQL_Str := 'Select * From 部门信息表 Order By 部门名称'; 
      TempQuery.Active := False; 
      TempQuery.SQL.Clear; 
      TempQuery.SQL.Add(SQL_Str); 
      TempQuery.Open; 
      cxLookupComboBox1.Properties.KeyFieldNames := '部门名称'; 
      cxLookupComboBox1.Properties.ListFieldNames := '部门名称'; 
    end; 
  end; 
end; 
 
procedure TReportOfPrintRecForm.BitBtn4Click(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TReportOfPrintRecForm.FormClose(Sender: TObject; 
  var Action: TCloseAction); 
begin 
  Action := CaFree; 
end; 
 
procedure TReportOfPrintRecForm.FormCreate(Sender: TObject); 
begin 
  cxDateEdit1.Date := Date - 30; 
  cxDateEdit2.Date := Date; 
end; 
 
procedure TReportOfPrintRecForm.cxDateEdit1Exit(Sender: TObject); 
begin 
  if (cxDateEdit1.Text = '') then 
  begin 
    cxDateEdit1.Date := Date - 30; 
  end; 
end; 
 
procedure TReportOfPrintRecForm.cxDateEdit2Exit(Sender: TObject); 
begin 
  if (cxDateEdit2.Text = '') then 
  begin 
    cxDateEdit2.Date := Date; 
  end; 
end; 
 
procedure TReportOfPrintRecForm.BitBtn5Click(Sender: TObject); 
var 
  myexcel: variant; 
  workbook: olevariant; 
  worksheet: olevariant; 
  i, j, n: integer; 
begin 
  if (InfoQuery.RecordCount > 0) then 
  begin 
    try 
      myexcel := createoleobject('excel.application'); 
      myexcel.application.workbooks.Add; 
      myexcel.application.visible := true; 
      workbook := myexcel.application.workbooks[1]; 
      worksheet := workbook.worksheets.item[1]; 
    except 
      ShowMessage('你没有安装微软的EXCEL2000以上程序!'); 
    end; 
    i := 1; //EXECL表行号 
    j := 1; //EXECL表列号 
    InfoQuery.First; 
    for n := 0 to InfoQuery.FieldCount - 1 do 
    begin 
      worksheet.Cells(i, j) := InfoQuery.fields[n].DisplayLabel; //在EXCEL第一行写上字段名。 
      j := j + 1; 
    end; 
    InfoQuery.First; 
    while not InfoQuery.eof do 
    begin 
      inc(i); 
      for j := 0 to InfoQuery.FieldCount - 1 do 
      begin 
        worksheet.Cells[i, j + 1] := InfoQuery.fields[j].AsString; 
      end; 
      InfoQuery.next; 
    end; 
  end 
  else 
  begin 
    ShowMessage('没有记录可以导出,请先统计出数据再进行导出!'); 
  end; 
end; 
 
procedure TReportOfPrintRecForm.BitBtn2Click(Sender: TObject); 
var 
  BeginDate, EndDate, SQL_Str: string; 
begin 
  BeginDate := #39 + cxDateEdit1.Text + #39; 
  EndDate := #39 + cxDateEdit2.Text + #39; 
  if (cxLookupComboBox1.Text = '') then 
  begin 
    SQL_Str := 'SELECT 部门名称, 姓名, 计算机名称, 使用打印机, 文件名称, 页数, 日期 FROM 打印记录表' 
      + ' Where (日期>=' + BeginDate + ') And (日期<=' + EndDate + ') Order By 日期'; 
    InfoQuery.Active := False; 
    InfoQuery.SQL.Clear; 
    InfoQuery.SQL.Add(SQL_Str); 
    InfoQuery.Open; 
  end 
  else 
  begin 
    if (ComboBox1.Text = '姓名') then 
    begin 
      SQL_Str := 'SELECT 部门名称, 姓名, 计算机名称, 使用打印机, 文件名称, 页数, 日期 FROM 打印记录表' 
        + ' Where (日期>=' + BeginDate + ') And (日期<=' + EndDate + ') And (姓名=' + #39 + cxLookupComboBox1.Text + #39 + ') Order By 日期'; 
      InfoQuery.Active := False; 
      InfoQuery.SQL.Clear; 
      InfoQuery.SQL.Add(SQL_Str); 
      InfoQuery.Open; 
    end 
    else 
      if (ComboBox1.Text = '部门') then 
    begin 
      SQL_Str := 'SELECT 部门名称, 姓名, 计算机名称, 使用打印机, 文件名称, 页数, 日期 FROM 打印记录表' 
        + ' Where (日期>=' + BeginDate + ') And (日期<=' + EndDate + ') And (部门=' + #39 + cxLookupComboBox1.Text + #39 + ') Order By 日期'; 
      InfoQuery.Active := False; 
      InfoQuery.SQL.Clear; 
      InfoQuery.SQL.Add(SQL_Str); 
      InfoQuery.Open; 
    end; 
  end; 
end; 
 
end.