Recent Posts
Selamat datang di Coding Delphi Land Weblog kumpulan source code pemogram delphi
(Bukan maksud untuk menggurui tetapi marilah kita berbagi ilmu tuk perkembangan kemajuan teknologi kita
Minggu, 15 November 2009
Export Database To Excel pada Delphi
Uses .........,excel97;
var
Form1: TForm1;
excel : variant;
excel1 : variant;
procedure Tform1.dbgirdtostring(db:tdbgrid;str:tstringgrid) ;
var ta : tdataset;
i,j : integer;
begin
ta := db.DataSource.DataSet;
if ta.Filtered then
ta.FindFirst
else
ta.First;
j:=0;
str.rowcount :=1;
str.fixedcols:=0;
str.ColCount:= db.Columns.Count;
for i:=0 to db.Columns.Count-1 do
begin
str.Cells[i,0]:= db.Columns.Items[i].FieldName;
end;
while not ta.Eof do
begin
j:=j+1;
str.RowCount:=str.RowCount+1;
for i:=0 to db.Columns.Count-1 do
str.Cells[i,j]:=db.Columns.Grid.Fields[i].AsString;
ta.Next;
end;
if str.RowCount>1 then str.FixedRows:=1
end;
procedure TForm1.exporttoexcel;
var i,j,jums,jumlahdata: integer;
proy, model : string;
begin
adoquery1.Active := false;
adoquery1.SQL.Clear;
with adoquery1 do
begin
Close;
SQL.Clear;
SQL.Add('select * '+'from table1');
ExecSQL;
Open;
end;
Excel := CreateOleObject('Excel.Application');
excel.workbooks.Open(File Excel); // file excel contoh : c:\\coba.xls
excel.Visible := true;
dbgirdtostring(DBGrid1, StringGrid1);
jumlahdata := adoquery1.RecordCount;
for i := 0 to jumlahdata+1 do
begin
for j := 1 to 9 do
begin
excel.activesheet.Cells[i + 2, 5].Value := StringGrid5.Cells[1, i+1];
end;
end;
end;
0 komentar:
Posting Komentar