IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bases de données Delphi Discussion :

Importer donnees texte d'une page Excel vers la base Paradox


Sujet :

Bases de données Delphi

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut Importer donnees texte d'une page Excel vers la base Paradox
    Bonjour,
    Voilà mon problème:
    J'ai un classeur excel c:\classeur1.xls. Dedans, la première, la deuxième et la troisième colonne sont remplises des données (texte). Ces données je souhaite importer dans mon application dans la base Paradox7: première colonne (A) dans la Table1.nom, deuxième colonne (B) dans la Table1.prenom et le troisième colonne (C) dans Table1.ville.

    Je vous remercie d'avance pour toute bonne volonté!

  2. #2
    Membre expérimenté
    Avatar de Frank
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Avril 2002
    Messages
    1 095
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Chef de projet Informatique
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 095
    Points : 1 392
    Points
    1 392
    Par défaut
    Merci d'utiliser la recherche avancée, les tutoriels et la .

    Ta question n'est pas nouvelle , elle a déjà été abordé.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    Oui, mais je ne trouve pas la réponse à cette question. Tout est approximatif, mais je ne suis pas un programmeur pro, mais plutôt débutant, donc il est difficile pour moi d'adapter ça...
    Peux-tu me faire un exemple simple qui marche par simple copier-coller?
    Merci...

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    J'ai trouvé ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    var tsl1,tsl2 : tstringlist ; 
    begin
    tsl1:=Tstringlist.create ; 
    tsl2:=Tstringlist.create ; 
    tsl2.delimiter:=';' ; // ou un autre caractère comme TAB 
    Tstringlist.loadfromfile('c:\classeur1.xls') ; 
    For i:=0 to tsl1.count-1 do begin 
        tsl2.delimitedText:=tsl1[i] ; 
        champA:=AnsiquotedStr(tsl2[0]) ; 
        champB:=AnsiquotedStr(tsl2[1]) ; 
        end; 
    end;
    mais j'ai toujours une erreur...

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    J'ai ce programme qui : ouvre une page excel, lit et ecrit dans la première cellule (A1) du classeur. Mais comment faire une boucle pour qu'il lit toute la colonne A et transfère ça vers Delphi (même dans un listbox1!), après je peux me débrouiller.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleServer, Excel2000, ActiveX, comobj, StdCtrls;
     
    type
      TForm1 = class(TForm)
        OpenXls: TButton;
        btnCloseExcel: TButton;
        btnWriteCell: TButton;
        btnReadCell: TButton;
        XlsApp: TExcelApplication;
        edWCell: TEdit;
        edRCell: TEdit;
        procedure OpenXlsClick(Sender: TObject);
        procedure btnCloseExcelClick(Sender: TObject);
        procedure btnWriteCellClick(Sender: TObject);
        procedure btnReadCellClick(Sender: TObject);
      private
        { Déclarations privées }
        function XlsInit(var lcid: integer; var XlsApp: TExcelApplication; var XlsWork: _workbook; var XlsPage: _Worksheet; 
                               sFile, sNameWork: string; bVisible: boolean): boolean; 
          procedure XlsClose(XlsApp: TExcelApplication; XlsWork: _workbook; bSave: boolean); 
          function Xls_RCell(Line, Col: integer): variant; 
          procedure Xls_WCell(Line, Col: integer; Value: variant);
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
    var
      lcid: integer; 
       XlsWBook: _workbook;   // classeur 
       XlsPage: _Worksheet;   // Feuille 
     
     ////////////////
     // lancer / ouvrir fichier XLS 
    function TForm1.XlsInit(var lcid: integer; var XlsApp: TExcelApplication; var XlsWork: _workbook; var XlsPage: _Worksheet; 
                               sFile, sNameWork: string; bVisible: boolean): boolean; 
     begin 
       Result := true; 
       CoInitialize(nil); 
       TRY 
          lcid := GetUserDefaultLCID;          // Identifiant local 
          XlsApp.Visible[lcid]:= bVisible;    // lance Excel 
          XlsWork := XlsApp.Workbooks.Open(sFile, False,False, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
                                                           EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,0); 
       EXCEPT 
          ShowMessage('Excel indisponible, ou fichier occupé... Ré-essayer plus tard !'); 
          if XlsWork <> nil then 
             XlsApp.Quit; 
          Result := false; 
          Exit; 
       END; 
       XlsPage := XlsWork.Worksheets[sNameWork] as _worksheet; 
     end; 
    ///////////////////////////
    procedure TForm1.OpenXlsClick(Sender: TObject);
    var 
       sFile: string; 
       bVisibleXls: boolean; 
       sNameFeuille: string; 
     begin 
       // Lancer Excel + Open Xls 
       sFile :=    ExtractFilePath(Application.ExeName) + 'Classeur1.XLS'; 
       bVisibleXls := true;   // Voir Excel 
       sNameFeuille := 'Feuil1'; 
       XlsInit(lcid, XlsApp, XlsWBook, XlsPage, sFile, sNameFeuille, bVisibleXls); 
     end;
     
     
    procedure TForm1.btnCloseExcelClick(Sender: TObject);
    var 
       bSaveXls: boolean; 
     begin 
       // Fermer Excel
       bSaveXls := false;   // pas de sauvegarde 
       XlsClose(XlsApp, XlsWBook, bSaveXls); 
     end; 
     
     
    procedure TForm1.btnWriteCellClick(Sender: TObject);
    var 
       iLine, iCol: integer;
       oValue: variant; 
     begin 
       //Insére du texte dans la première cellule [1,1] 
       iLine := 1;
       iCol := 1;
       ovalue := edWCell.Text; 
       Xls_WCell(iLine, iCol, oValue); 
     end; 
     
     
    procedure TForm1.btnReadCellClick(Sender: TObject);
    var
       iLine, iCol: integer;
     begin
       // lecture d'une cellule
       iLine := 1;
       iCol := 1;
       edRCell.Text := Xls_RCell(iLine, iCol);
     end;
     
    // fermer Excel
    procedure TForm1.XlsClose(XlsApp: TExcelApplication; XlsWork: _workbook; bSave: boolean);
     begin
       XlsWork.Close(bSave, emptyparam, emptyparam, lcid);
       XlsApp.Quit;
     end;
     
    // Lecture d'une cellule
    function TForm1.Xls_RCell(Line, Col: integer): variant;
     begin 
       Result := XlsPage.Cells.Item[Line, Col].Value;
     end;
     
    // Ecriture dans une cellule
    procedure TForm1.Xls_WCell(Line, Col: integer; Value: variant); 
     begin 
       if Value <> NULL then 
          XlsPage.Cells.Item[Line, Col] := Value; 
     end; 
     
    end.
    Merci encore.

  6. #6
    Membre actif

    Profil pro
    Personnel
    Inscrit en
    Septembre 2003
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France, Meuse (Lorraine)

    Informations professionnelles :
    Activité : Personnel

    Informations forums :
    Inscription : Septembre 2003
    Messages : 142
    Points : 210
    Points
    210
    Par défaut
    Salut,
    Ben, tu fais une boucle de lecture...
    Sur la base du code précédent (j'en suis l'auteur ;o) )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    // à déclarer en Private
    procedure SaveInBD(sNom, sPNom, sVille: string);
    ...
    Implementation
    ...
    procedure TForm1.Button1Click(Sender: TObject);
     var
      iLine, iCol: integer;
      bFin: boolean;
      sFile: string;
      bVisibleXls, bSaveXls: boolean;
      sNameFeuille: string;
      sNom, sPNom, sVille: string;
     begin
       // Lancer Excel + Open Xls
       sFile :=    ExtractFilePath(Application.ExeName) + 'Classeur1.XLS';
       bVisibleXls := true;   // Voir Excel
       sNameFeuille := 'Feuil1';  // 1ère feuille
       XlsInit(lcid, XlsApp, XlsWBook, XlsPage, sFile, sNameFeuille, bVisibleXls);
      //
      Table1.Open;
      bFin := false;
      iLine := 1;
      repeat
        // lire les données
        sNom := Xls_RCell(iLine, 1);
        sPNom := Xls_RCell(iLine, 2);
        sVille := Xls_RCell(iLine, 3);
        if sNom = '' then
          Break;  // fin des données XLS
        // met à jour la table
        SaveInBD(sNom, sPNom, sVille);
        Inc(iLine);  // ligne suivante dans XLS
      Until bFin;
      // ferme Excel
      bSaveXls := false;   // pas de sauvegarde
      XlsClose(XlsApp, XlsWBook, bSaveXls);
     end;
     
    procedure TForm1.SaveInBD(sNom, sPNom, sVille: string);
     var
      i: integer;
     begin
      with Table1 do begin
        SetKey;
        FieldByName('Nom'). asString := sNom;
        FieldByName('Prénom'). asString := sPNom;
        if GotoKey then begin
          // données existantes
          i := 1;
          Repeat
            SetKey;
            FieldByName('Nom'). asString := sNom + ' '+ IntToStr(i);
            FieldByName('Prénom'). asString := 'sPNom;
            Inc(i);
          Until not Gotokey 
          sNom := sNom + ' '+ IntToStr(i);  // on n'écrase pas, on rajoute 1, 2, ...
        end;
        // ajoute
        Append;
        FieldByName('Nom'). asString := sNom;
        FieldByName('Prénom'). asString := sPNom;
        FieldByName('Ville'). asString := sVille;
        Post;
      end;  // with
     end;
    Je l'ai codé de tête sans vérif, mais cela doit coller...
    @+
    Fabrice

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    Je suis content que tu sois là...
    J'ai trop d'érreurs, j'ai réussi à corriger quelque chose, mais toujours nouveaux erreurs...

  8. #8
    Membre actif

    Profil pro
    Personnel
    Inscrit en
    Septembre 2003
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France, Meuse (Lorraine)

    Informations professionnelles :
    Activité : Personnel

    Informations forums :
    Inscription : Septembre 2003
    Messages : 142
    Points : 210
    Points
    210
    Par défaut
    Trop d'erreurs ? Explique toi...
    J'ai essayé, il faut corriger déjà cette procédure:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    procedure TForm1.SaveInBD(sNom, sPNom, sVille: string);
     var
      i: integer;
     begin
      with Table1 do begin
        SetKey;
        FieldByName('nom'). asString := sNom;
        FieldByName('prenom'). asString := sPNom;
        if GotoKey then begin
          // données existantes
          i := 1;
          Repeat
            SetKey;
            FieldByName('nom').asString := sNom + ' ' + IntToStr(i);
            FieldByName('prenom').asString := 'sPNom';
            Inc(i);
          Until not Gotokey;
          sNom := sNom + ' '+ IntToStr(i);  // on n'écrase pas, on rajoute 1, 2, ...
        end;
        // ajoute
        Append;
        FieldByName('nom').asString := sNom;
        FieldByName('prenom').asString := sPNom;
        FieldByName('ville').asString := sVille;
        Post;
      end;  // with 
     end;
    La boucle de lecture est OK. Tu peux essayer une autre méthode sans passer par les procedures XlsInit, ...:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    procedure TForm1.Button1Click(Sender: TObject);
     var
      XlsWk: 	_workbook;
      XlsPage :_Worksheet;
      sFile: string;
      sNom, sPNom, sVille: string;
      bFin: boolean;
      iLine: integer;
     begin
      XlsApp.Connect;
      XlsApp.Visible[0] := true;
      sFile := ExtractFilePath(Application.ExeName) + 'Classeur1.XLS'; 
      XlsWk := XlsApp.Workbooks.Open(sFile, False, False, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
      XlsPage := XlsWk.Worksheets['Feuil1'] as _worksheet;
      Table1.Open;
      bFin := false;
      iLine := 1;
      repeat
        // lire les données
        sNom := XlsPage.Cells.Item[iLine, 1].Value;
        sPNom := XlsPage.Cells.Item[iLine, 2].Value;
        sVille := XlsPage.Cells.Item[iLine, 3].Value;
        if sNom = '' then
          Break;  // fin des données XLS
        // met à jour la table
        SaveInBD(sNom, sPNom, sVille);
        Inc(iLine);  // ligne suivante dans XLS
      Until bFin;
     end;
    Fabrice

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    J'ai une petite erreur (exception de débugeur):
    Le champ Prenom n'est pas indexé et ne peut être modifié
    Mais, c'est pas grave!

    Donc, j'ai supprimé "prénom" et "ville", et ça marche maintenant TRES BIEN, je te remercie, surtout de ta patience, car c'est n'est pas la première fois que je t'embête avec mes problèmes.

    Maintenant, je vais continuer avec mes problèmes: pour simplifier tout ça, j'ai une petite idée: comment faire que sur un click de bouton, une fois la feuille excel ouverte, logiciel parcourt tous les lignes de la première colonne (A) de cette feuille excel, puis compare les enregistremment dans la base (table1) avec la valeur "nom" et importe uniquement les lignes de la feuille excel qui n'existent pas déjà dans table1.nom?

    Merci d'avance à Fabrice.

  10. #10
    Membre actif

    Profil pro
    Personnel
    Inscrit en
    Septembre 2003
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France, Meuse (Lorraine)

    Informations professionnelles :
    Activité : Personnel

    Informations forums :
    Inscription : Septembre 2003
    Messages : 142
    Points : 210
    Points
    210
    Par défaut
    Salut,
    Pour le message d'erreur, cela signifie que ta table n'est pas indexée...
    Un index (primaire) permet d'avoir la table triée en permanence sur les champs définis dans l'index. Cela évite surtout d'avoir des enregistrements en double (doublons).
    Cherche dans l'aide ou sur développez.com, il y a suffisamment d'infos...
    @+
    Fabrice

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Août 2005
    Messages
    228
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 228
    Points : 72
    Points
    72
    Par défaut
    Merci Fabrice, c'est bon, enfin!!
    @ bientôt!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [WD15] Extraction texte d'une page html vers table windev
    Par bouscape dans le forum WinDev
    Réponses: 4
    Dernier message: 19/01/2012, 16h09
  2. Importer de texte d'une page html
    Par chicod83 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 28/05/2008, 15h52
  3. Importer des données d'une feuille Excel vers TABLE
    Par souminet dans le forum Bases de données
    Réponses: 1
    Dernier message: 07/11/2006, 09h10
  4. [VBA-E] Exporter une page Excel vers un Fichier Txt
    Par mamantins dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 08/06/2006, 11h09
  5. Importer le contenu d'une cellule Excel vers Access
    Par deaqu1 dans le forum Access
    Réponses: 7
    Dernier message: 05/11/2005, 15h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo