Wednesday, 7 April 2010

Export an Interbase Table into an MS Excel File

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 :

ActiveTrue
DatabaseNameALIASMYDATA
NameMyTable
TableNameEMPLOYEE

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 )


7 comments:

  1. waw...mantaf...dapet tutorial excel...yg dulu sempet terlupakan

    ReplyDelete
  2. ini baru tutorial exel, kebetulan pakde belum pernah belajar exel, jadi sangat berguna bagi pakde

    maksih ya atas sharingnya.

    ReplyDelete
  3. @Pakde : thanks pakde for visiting my blog, I'm reading your tips, it encourages me to keep posting.

    ReplyDelete
  4. Bang 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
  5. @ 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.

    ReplyDelete
  6. thanks 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

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.