Lecture cellules Excel

Résolu
YB28 Messages postés 38 Date d'inscription lundi 23 avril 2012 Statut Membre Dernière intervention 29 juillet 2012 - 8 juin 2012 à 14:29
YB28 Messages postés 38 Date d'inscription lundi 23 avril 2012 Statut Membre Dernière intervention 29 juillet 2012 - 8 juin 2012 à 16:40
Bonjour,
Je souhaite effectuer un programme sous delphi7 permettant de récupérer des données de plusieurs cellules d'un tableur excel et de les ajouter dans un mémo. Cependant j'obtien un message d'erreur EVariantInvalidOpError lorsque j'effectue ce code:

procedure TForm1.bt_executerClick(Sender: TObject);
var i,j     : integer;
begin
{ouverture d'Excel}
  ExcelApplication1.Visible[0]:=true; // pour rendre Excel visible

{Ouverture du fichier}
ExcelApplication1.Workbooks.Open(Edit1.Text,EmptyParam, True, EmptyParam, EmptyParam, EmptyParam,
                       EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                       EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);

{Lire la valeur d'une cellule}
  for i:=strtoint(Edit5.text) to strtoint(Edit6.text) do
  begin
    for j:=1 to 25 do
    begin
      Memo1.Text:=Memo1.Text+ExcelApplication1.Cells.Item[i,j];// probleme ici sans doute..
    end;
  end;
end;

Je ne comprend pas ...
Merci ;)
YB

7 réponses

cs_walidlam Messages postés 107 Date d'inscription lundi 30 mai 2011 Statut Membre Dernière intervention 29 avril 2013 1
8 juin 2012 à 16:04
salut!!
normalement le code sera comme suit:
var i,j     : integer;
 ExcelApplication1:variant;
 workbooks:variant;
begin
{ouverture d'Excel}
 // ExcelApplication1.Visible[0]:=true; // pour rendre Excel visible

{Ouverture du fichier}

 ExcelApplication1:= CreateOleObject('Excel.Application');
 //ExcelApplication1.visible:=true;
 ExcelApplication1.Workbooks.Open(edit1.Text);

{Lire la valeur d'une cellule}
  for i:=strtoint(Edit5.text) to strtoint(Edit6.text) do
  begin
    for j:=1 to 25 do
    begin
    if ExcelApplication1.Cells[i,j].Formula <> ''then
//pour eleminer les cellules vides
      Memo1.Text:=Memo1.Text+'-'+ExcelApplication1.Cells[i,j].Formula ;// probleme ici sans doute..

    end;
 // end;
      end;
end;

@+
3
Rejoignez-nous