Nah untuk bagian ke-2 ini saya akan menuliskan listing program uintuk meng-eksport data dari DBGrid ke Excel, jadi diharapkan aplikasi yang kawan buat hasil proses maupun output yang akan di cetak bisa di cetak kedalam excel..
Dan ini adalah listing nya..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj, StdCtrls, DB, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
Excel: TButton;
procedure ExcelClick(Sender: TObject);
private
{ Private declarations }
XlApp, XlBook, XlSheet, XlSheets, Range,chat : Variant; // Excel 97
WApp, Word : Variant; // Word 97
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ExcelClick(Sender: TObject);
var i,x:integer;
Sfile:string;
begin
// buka excel
XlApp := CreateOleObject('Excel.Application');
// tambahkan workbook
XlBook := XlApp.WorkBooks.Add;
// tambahkan worksheet
XlSheet := XlBook.worksheets.add;
//cetak header field dari dbgrid
for i:=0 to dbgrid1.FieldCount-1 do
begin
XlSheet.cells[2,i+1].value:=dbgrid1.columns[i].Title.Caption;
end;
// transfer data ke excel
table1.First;
x:=1;
while not table1.Eof do
begin
for i:=0 to dbgrid1.FieldCount-1 do
begin
XlSheet.cells[2+x,i+1].value:=dbgrid1.Fields[i].Text;
end;
table1.Next;
inc(x);
end;
//menampilkan aplikasi //XlApp.visible:=true;
//script dibawah ini untuk dialog disimpan atau ditampilkan
if MessageDlg('Apakah hasil export ditampilkan..?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
MessageDlg('Hasil Ditampilkan', mtInformation,
[mbOk], 0);
XlApp.visible:=true;
end
else
//simpan ke file
begin
Sfile:= InputBox('Nama File', 'hasil export', 'c:\hasil.xls');
XlApp.ActiveWorkbook.SaveAs(sfile);
XlApp.visible:=true;
end
end;
end.
Dan ini adalah listing nya..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj, StdCtrls, DB, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
Excel: TButton;
procedure ExcelClick(Sender: TObject);
private
{ Private declarations }
XlApp, XlBook, XlSheet, XlSheets, Range,chat : Variant; // Excel 97
WApp, Word : Variant; // Word 97
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ExcelClick(Sender: TObject);
var i,x:integer;
Sfile:string;
begin
// buka excel
XlApp := CreateOleObject('Excel.Application');
// tambahkan workbook
XlBook := XlApp.WorkBooks.Add;
// tambahkan worksheet
XlSheet := XlBook.worksheets.add;
//cetak header field dari dbgrid
for i:=0 to dbgrid1.FieldCount-1 do
begin
XlSheet.cells[2,i+1].value:=dbgrid1.columns[i].Title.Caption;
end;
// transfer data ke excel
table1.First;
x:=1;
while not table1.Eof do
begin
for i:=0 to dbgrid1.FieldCount-1 do
begin
XlSheet.cells[2+x,i+1].value:=dbgrid1.Fields[i].Text;
end;
table1.Next;
inc(x);
end;
//menampilkan aplikasi //XlApp.visible:=true;
//script dibawah ini untuk dialog disimpan atau ditampilkan
if MessageDlg('Apakah hasil export ditampilkan..?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
MessageDlg('Hasil Ditampilkan', mtInformation,
[mbOk], 0);
XlApp.visible:=true;
end
else
//simpan ke file
begin
Sfile:= InputBox('Nama File', 'hasil export', 'c:\hasil.xls');
XlApp.ActiveWorkbook.SaveAs(sfile);
XlApp.visible:=true;
end
end;
end.
Post a Comment