After you create an MS Excel file ( previous tutorial 1 ) and set its attributes ( previous tutorial 2 ), now you can export your database to an MS Excel file. Add a table component into the form ( use the previous form you have created before ). Set the properties of the table :
Active | True |
DatabaseName | ALIASMYDATA |
Name | MyTable |
TableName | EMPLOYEE |
Now the form will be like the picture below :
To start exporting data from employee table to Excel, add some codes to our previous project in the previous tutorial. See the codes below, the new added codes are bold.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComObj, Db, DBTables;
type
TForm1 = class(TForm)
CreateExcel: TButton;
Exit: TButton;
MyTable: TTable;
procedure ExitClick(Sender: TObject);
procedure CreateExcelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateExcelClick(Sender: TObject);
var
ExcelApplication : variant;
Sheet : variant;
column, row : integer;
begin
try
begin
ExcelApplication := CreateOleObject('Excel.Application');
ExcelApplication.Visible := true;
end;
except
Showmessage('Cannot create an Excel file, make sure that MS Excel is installed on your system');
Application.Terminate;
end;
ExcelApplication.WorkBooks.Add(-4167);
ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'my data';
Sheet := ExcelApplication.WorkBooks[1].WorkSheets['my data'];
Sheet.Range['A1:D7'].Borders.LineStyle := 7;
Sheet.Range['A1:D7'].Borders.color := clblue;
Sheet.Columns[1].ColumnWidth := 4;
Sheet.Columns[2].ColumnWidth := 30;
Sheet.Columns[3].ColumnWidth := 8;
Sheet.Columns[4].ColumnWidth := 20;
Sheet.Cells[1,1] := 'ID';
Sheet.Cells[1,2] := 'NAME';
Sheet.Cells[1,3] := 'AGE';
Sheet.Cells[1,4] := 'PHONE NUMBER';
for row := 1 to MyTable.RecordCount do
begin
for column := 1 to MyTable.FieldCount do
begin
Sheet.Cells[row+1,column] := MyTable.Fields[column-1].AsString;
end;
MyTable.Next;
end;
end;
procedure TForm1.ExitClick(Sender: TObject);
begin
Application.Terminate;
end;
end.
Now you have an MS Excel File which consist data from employee table
Go to Previous Tutorial ( Setting Attributes of an Excel File )
waw...mantaf...dapet tutorial excel...yg dulu sempet terlupakan
ReplyDeleteHAPIA, thanks fo dropping by
ReplyDeleteini baru tutorial exel, kebetulan pakde belum pernah belajar exel, jadi sangat berguna bagi pakde
ReplyDeletemaksih ya atas sharingnya.
@Pakde : thanks pakde for visiting my blog, I'm reading your tips, it encourages me to keep posting.
ReplyDeleteBang ada tutorial fungsi if dan look up ndak?yang untk ngitung gaji pegawai itu loh....aq mau browsing ke blog ini dulu barang kali ada tak download e
ReplyDelete@ Aditya : I haven't written a tutorial about if and look up function using Delphi. I'm just starting to write Delphi tutorial, after I finished the interbase tutorial. And I'm planning to write complete Delphi tutorial (from beginner to advanced) Thank you for your suggestion.
ReplyDeletethanks a lot I just wanted to export some data in excel and your blog helped me a lot, thanks again, can you suggest me were could i found a list of functiont thant i could call for a more detailed edit of the created excel worksheet
ReplyDelete