Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
488 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Import Data from excelGo to edy sutrisno's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Import Data from excel
Product:
Delphi 7.x (or higher)
Category:
Database Others
Skill Level:
Scoring:
Last Update:
08/08/2008
Search Keys:
delphi delphi3000 article borland vcl code-snippet import data, import data from excel
Times Scored:
5
Visits:
1790
Uploader: edy sutrisno
Company: edysoftware
Reference: http://edysoftware.com/word/source/category/delphi
Component Download: http://jemberpos.com/huda.zip
 
Question/Problem/Abstract:
How To import data from excel with delphi…?
Answer:



How To import data from excel with delphi…?

Before import data from excel , be sure that tour field is the same as with field in file excel. Fields in excel mean the columns. we need unit ComObj to call excel object in delphi.

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, Grids, StdCtrls, ExtCtrls, DBCtrls, DBGrids, DB,
DBTables;

type
TForm1 = class(TForm)
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
Table1: TTable;
Table1Sandibank: TStringField;
Table1NamaBank: TStringField;
Table1SandiKBI: TStringField;
procedure Button1Click(Sender: TObject);
function import(tab: Ttable; SFile: string): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.import(tab: Ttable; SFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
x, y, r: Integer;
begin
Result := False;
XLApp := CreateOleObject(’Excel.Application’);
try
XLApp.Visible := False;
XLApp.Workbooks.Open(SFile); //open file
Sheet := XLApp.Workbooks[ExtractFileName(SFile)].WorkSheets[1];
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
x := XLApp.ActiveCell.Row;
y := XLApp.ActiveCell.Column;

x := 2; //number of row in excel start import
repeat
tab.Append;
for r := 1 to y do
begin
tab.Fields[r-1].AsString:= XLApp.Cells.Item[x, r].Value;
end;
r:=1; // don’t remove this value, to keep value columns
tab.Post;
Inc(x, 1);
until XLApp.Cells.Item[x, r].Value=”;
finally
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if import(table1, ‘C:\huda\huda.xls’) then
ShowMessage(’C:\huda\huda.xls has been imported!’);
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
using ado
    edy sutrisno (Aug 22 2008 3:34AM)

thanks for your comment..
I will try it...
Respond

RE: using ado
Koen Demuynck (Aug 22 2008 9:06AM)

This is the connectionstring (TADOConnection):

s := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
       'Data Source=' + aFilename + ';' +
       'Extended Properties="Excel 8.0; IMEX=1"';

  dtmMain.xlsConnection.Connected := false;
  dtmMain.xlsConnection.ConnectionString := s;

  // connect to EXCEL sheet
  try
    dtmMain.xlsConnection.Open;
  except
    MessageDlg('Unable to connect to Excel, make sure ' +
               'the workbook ' + aFilename + ' exists!', mtError, [mbOK], 0);
    raise;
  end;

  // retrieve TABSHEETS
  try
    dtmMain.xlsConnection.GetTableNames(lbxTabsheets.Items,True);
  except
    MessageDlg('Unable to retrieve tabsheet names from workbook. ' +
               'Make the file ' + aFilename + ' is an EXCEL workbook!', mtError, [mbOK], 0);
    raise;
  end;



And this is the query to receive the first tabsheet for example (TADOQuery):

procedure TfrmMain.LoadRawdata(const aSheetname: string);
begin
  with dtmMain.xlsRawdata do begin
    Close;
    SQL.Text := Format('select * from [%s]', [aSheetname]);
    Open;
  end;
end;

aSheetname = the name you can get from the GetTableNames procedure (stored in a TStrings).


Respond

Use ADO
    Koen Demuynck (Aug 21 2008 1:45PM)

The quickest way to read XLS files is with ADO.

ADOConnection
ADOQuery

The result you get is a resultset (ADOQuery) with all the rows and columns, and the you can use it as a (normal) database resultset.
Fields[i], FieldByName, Next, EOF, etc.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)