this slowpoke moves

Export DBGrid to Excel

uses ComObj, DB, DBTables, Grids, DBGrids,
  ExtCtrls, ComCtrls, TabNotBk
  
//

// Export to Excel
procedure TForm1.Button2Click(Sender: TObject);
var
 XL, XArr: Variant;
 i: Integer;
 j: Integer;
begin
 XArr:=VarArrayCreate([1,TableDB.FieldCount],varVariant);
 XL := CreateOLEObject('Excel.Application');
 XL.WorkBooks.add;
 XL.visible:=true;

 j := 1;
 TableDB.First;
 while not TableDB.Eof do
  begin
   i:=1;
   while i<=TableDB.FieldCount do
    begin
     XArr[i] := TableDB.Fields[i-1].Value;
     i := i+1;
    end;
   XL.Range['A'+IntToStr(j),
   CHR(64+TableDB.FieldCount)+IntToStr(j)].Value := XArr;
   TableDB.Next;
   j:=j+1;
  end;
 XL.Range['A1',CHR(64+TableDB.FieldCount)+IntToStr(j)].select;
 // XL.cells.select;                     
 XL.Selection.Font.Name:='Arial cur';
 XL.Selection.Font.Size:=10;
 XL.selection.Columns.AutoFit;
 XL.Range['A1','A1'].select;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName);
end;

// Laden einer DBGrid Base
procedure TForm1.Button1Click(Sender: TObject);
begin
 if OpenDialog1.Execute
 then
  begin
   TableDB.Active:=false;
   TableDB.TableName := OpenDialog1.FileName;
   TableDB.Active:=true;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate