Get data from excel file

Posted in Others

This example shows, how to run Excel, open *.xls file and get data of this file into StringGrid component. Don't forget add ComObj in uses chapter.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Excel:=CreateOleObject('Excel.Application');
  Excel.Visible:=True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Excel.Workbooks.Open(GetCurrentDir+'\book1.xls');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Excel.Workbooks.Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  i, j: Integer;
begin
  for i:=1 to 4 do
    for j:=1 to 2 do
      StringGrid1.Cells[j,i]:=
        Excel.ActiveSheet.Cells[i,j].Value;
end;