unit Unit10;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, Buttons, ComObj;

type
  TForm10 = class(TForm)
    DataSource1: TDataSource;
    Query1: TQuery;
    Query2: TQuery;
    DBGrid1: TDBGrid;
    Query1Id: TIntegerField;
    Query1NazvaTovary: TStringField;
    Query1MarkaTovary: TStringField;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    GroupBox3: TGroupBox;
    BitBtn6: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form10: TForm10;

implementation



{$R *.dfm}

procedure TForm10.BitBtn1Click(Sender: TObject);
begin
  Edit1.Text:='';
  Edit2.Text:='';
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  Self.Tag:=1;
end;

procedure TForm10.BitBtn2Click(Sender: TObject);
begin
 Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  Self.Tag:=2;
  Edit1.Text:=Query1.FieldByName('NazvaTovary').AsString;
  Edit2.Text:=Query1.FieldByName('MarkaTovary').AsString;
end;

procedure TForm10.BitBtn4Click(Sender: TObject);
begin
If Form10.Tag=1 then begin
if (Edit1.text='') or (Edit2.text='') then
begin
Application.MessageBox('Введіть дані','Помилка',MB_OK+MB_ICONWARNING);
exit;
end;
 Query2.SQL.Clear;
 Query2.SQL.Add('INSERT INTO tovary (NazvaTovary,MarkaTovary) values('+QuotedStr(edit1.text)+','+QuotedStr(edit2.text)+')');
 Query2.ExecSQL;
 Query1.Close;
 Query1.Open; end;
If Form10.Tag=2 then
begin
 Query2.SQL.Clear;
 Query2.SQL.Add('Update tovary Set NazvaTovary='+QuotedStr(Edit1.Text)+', MarkaTovary='+QuotedStr(Edit2.Text)+' Where Id='+Query1.FieldByName('Id').AsString);
 Query2.ExecSQL;
 Query1.Close;
 Query1.Open;
 end;
  Edit1.Text:='';
  Edit2.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  DbGrid1.SetFocus();
end;

procedure TForm10.BitBtn5Click(Sender: TObject);
begin
  Edit1.Text:='';
  Edit2.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
end;

procedure TForm10.FormActivate(Sender: TObject);
begin
  Query1.Open();
  Edit1.Text:='';
  Edit2.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
end;

procedure TForm10.BitBtn6Click(Sender: TObject);

var
 XLApp,Sheet,Colum:Variant;
 index,index2,i,s:Integer;
begin
try
 XLApp:= CreateOleObject('Excel.Application');
 TQuery(DBGrid1.DataSource.DataSet).DisableControls;
 XLApp.Visible:=False;
 XLApp.Workbooks.Add(-4167);
 XLApp.Workbooks[1].WorkSheets[1].Name:=Self.Caption;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Columns;
 index:=1;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Rows;
 Colum.Rows[1].Font.Bold:=true;
 Colum.Rows[1].Font.Size:=10;
 Sheet:=XLApp.Workbooks[1].WorkSheets[Self.Caption];
 for I := 1 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Visible then
   begin
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size>0 then Colum.Columns[index].ColumnWidth:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size
   else Colum.Columns[index].ColumnWidth:=12;
   Sheet.Cells[1,index].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].DisplayLabel;
   Inc(index);
   end;
TQuery(DBGrid1.DataSource.DataSet).First;
 for i:=2 to TQuery(DBGrid1.DataSource.DataSet).RecordCount+1 do
  begin
  index2:=1;
   for s := 0 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount-1 do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[s].Visible then
   begin
   Sheet.Cells[i,index2].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[s].AsString;
   Inc(index2);
   end;
   TQuery(DBGrid1.DataSource.DataSet).Next;
  end;
XLApp.Range[XLApp.Cells[1, 1], XLApp.Cells[TQuery(DBGrid1.DataSource.DataSet).RecordCount+1,index-1]].Select;
XLApp.Selection.Borders.LineStyle:=1;
XLApp.Selection.WrapText:=True;
XLApp.Range['A1'].Select;
TQuery(DBGrid1.DataSource.DataSet).EnableControls;
XLApp.Visible:=true;
except
Application.MessageBox(PChar('Microsoft Ecxel не інстальований, або виникла інша помилка.'), 'Помилка!!!', MB_ICONWARNING+MB_OK);
  if VarIsEmpty(XLApp) = false then
  begin
    XLApp.Quit;
    XLApp := 0;
  end;
exit;
end;
end;


end.
