unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, Grids, DBGrids, Menus, ComCtrls, ToolWin, ImgList,
  StdCtrls, ExtCtrls, ComObj;

type
  TMainForm = class(TForm)
    DBGrid1: TDBGrid;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    CloseMenu: TMenuItem;
    N3: TMenuItem;
    TownBookMenuItem: TMenuItem;
    Help1: TMenuItem;
    AboutMenuItem: TMenuItem;
    ImageList1: TImageList;
    ToolBar1: TToolBar;
    NewButton: TToolButton;
    EditButton: TToolButton;
    DelButton: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    ToolButton6: TToolButton;
    N2: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    ToolBar2: TToolBar;
    Label1: TLabel;
    FindTelephoneEdit: TEdit;
    Label2: TLabel;
    FindEdit: TEdit;
    FindButton: TToolButton;
    DBGrid2: TDBGrid;
    Splitter1: TSplitter;
    ExcelButton: TToolButton;
    ToolButton2: TToolButton;
    N10: TMenuItem;
    Excel1: TMenuItem;
    PrintButton: TToolButton;
    procedure CloseMenuClick(Sender: TObject);
    procedure TownBookMenuItemClick(Sender: TObject);
    procedure AboutMenuItemClick(Sender: TObject);
    procedure NewButtonClick(Sender: TObject);
    procedure EditButtonClick(Sender: TObject);
    procedure DelButtonClick(Sender: TObject);
    procedure FindTelephoneEditChange(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure FindButtonClick(Sender: TObject);
    procedure ExcelButtonClick(Sender: TObject);
    procedure PrintButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses DataModuleUnit, TownBookUnit, AboutUnit, EditFormUnit, FindResultUnit,
  ReportFormUnit;

{$R *.dfm}

procedure TMainForm.CloseMenuClick(Sender: TObject);
begin
 Close;
end;

procedure TMainForm.TownBookMenuItemClick(Sender: TObject);
begin
 TownBookForm.ShowModal;
end;

procedure TMainForm.AboutMenuItemClick(Sender: TObject);
begin
 AboutForm.ShowModal;
end;

procedure TMainForm.NewButtonClick(Sender: TObject);
begin
 DataModule1.BookTable.Insert;
 EditRecordForm.ShowModal;
end;

procedure TMainForm.EditButtonClick(Sender: TObject);
begin
 EditRecordForm.ShowModal;
end;

procedure TMainForm.DelButtonClick(Sender: TObject);
begin
 if Application.MessageBox(PChar('Ты действительно хочешь удалить '+DataModule1.BookTableDSDesigner.AsString), 'Внимание!!!', MB_OKCANCEL)=id_OK then
  DataModule1.BookTable.Delete;
end;

procedure TMainForm.FindTelephoneEditChange(Sender: TObject);
begin
 if Length(FindEdit.Text)>0 then
  DataModule1.BookTable.Filtered:=true
 else
  DataModule1.BookTable.Filtered:=false;

 DataModule1.BookTable.Filter:='Фамилия>'''+FindEdit.Text+'''';
end;

procedure TMainForm.N8Click(Sender: TObject);
begin
 DataModule1.BookTable.Sort:='Фамилия ASC';
end;

procedure TMainForm.N9Click(Sender: TObject);
begin
 DataModule1.BookTable.Sort:='Город ASC';
end;

procedure TMainForm.FindButtonClick(Sender: TObject);
begin
 DataModule1.FindQuery.Active:=false;
 DataModule1.FindQuery.Parameters.ParamByName('Telephone').Value:=FindTelephoneEdit.Text;
 DataModule1.FindQuery.Active:=true;

 FindResultForm.ShowModal;
 DataModule1.BookTable.Refresh;
 DataModule1.TelephonTable.Refresh;
end;

procedure TMainForm.ExcelButtonClick(Sender: TObject);
var
 XLApp,Sheet,Colum:Variant;
 index,i:Integer;
begin
 XLApp:= CreateOleObject('Excel.Application');
 XLApp.Visible:=true;
 XLApp.Workbooks.Add(-4167);
 XLApp.Workbooks[1].WorkSheets[1].Name:='Отчёт';
 Colum:=XLApp.Workbooks[1].WorkSheets['Отчёт'].Columns;
 Colum.Columns[1].ColumnWidth:=20;
 Colum.Columns[2].ColumnWidth:=20;
 Colum.Columns[3].ColumnWidth:=20;
 Colum.Columns[4].ColumnWidth:=20;
 Colum.Columns[5].ColumnWidth:=20;

 Colum:=XLApp.Workbooks[1].WorkSheets['Отчёт'].Rows;
 Colum.Rows[2].Font.Bold:=true;
 Colum.Rows[1].Font.Bold:=true;
 Colum.Rows[1].Font.Color:=clBlue;
 Colum.Rows[1].Font.Size:=14;

 Sheet:=XLApp.Workbooks[1].WorkSheets['Отчёт'];
 Sheet.Cells[1,2]:='Телефонный справочник';
 Sheet.Cells[2,1]:='Фамилия';
 Sheet.Cells[2,2]:='Имя';
 Sheet.Cells[2,3]:='e-mail';
 Sheet.Cells[2,4]:='Город';
 Sheet.Cells[2,5]:='Дата рождения';

 index:=3;
 DataModule1.BookTable.First;
 for i:=0 to DataModule1.BookTable.RecordCount-1 do
  begin
   Sheet.Cells[index,1]:=DataModule1.BookTable.Fields.Fields[1].AsString;
   Sheet.Cells[index,2]:=DataModule1.BookTable.Fields.Fields[2].AsString;
   Sheet.Cells[index,3]:=DataModule1.BookTable.Fields.Fields[3].AsString;
   Sheet.Cells[index,4]:=DataModule1.BookTable.Fields.Fields[5].AsString;
   Sheet.Cells[index,5]:=FormatDateTime('dddddd', DataModule1.BookTable.Fields.Fields[6].AsDateTime);
   Inc(index);
   DataModule1.BookTable.Next;
  end;
end;

procedure TMainForm.PrintButtonClick(Sender: TObject);
begin
 ReportForm.QuickRep1.PreviewModal;
end;

end.
