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

Langage Perl Discussion :

copie le contenu d'un fichier xls dans un fichier txt


Sujet :

Langage Perl

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut copie le contenu d'un fichier xls dans un fichier txt
    bonjour,
    d'avance merci.
    d'abord je tien à préciser que je suis débutant.
    en fait je voudrais parser un fichier .xls et récupérer ces données pour les envoyées à une base de données MySQL.
    pour ce je procède comme suit copier le fichier .xls vers .txt puis parser le fichier .txt (ce que je sais faire)
    alors j'ai fais des recherche sur internet pour arriver à copier le contenu d'un .xls dans un .txt
    voila le code que j'ai trouvé
    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
    #!c:/perl/bin/perl -w
    use strict;
     
    use warnings;
    use Spreadsheet::ParseExcel;
    my @lignes=0;
    my $path = "./xls_txt.txt";
    die "fichier non trouve !\n" if (! -s $path);
    open(FIC, ">>$path" ) or die "Can't open file: $!";
    @lignes = <FIC>;
    my $oBook =
     
     
    Spreadsheet::ParseExcel::Workbook->Parse('./20070611 cheklist_massy.xls');
    my($iR, $iC, $oWkS, $oWkC);
    foreach my $oWkS (@{$oBook->{Worksheet}}) {
       print "--------- SHEET:", $oWkS->{Name}, "\n";
      for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
      $iR++) {
         for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
     $iC++) {
          $oWkC = $oWkS->{Cells}[$iR][$iC];
        print FIC "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
        }
      }
      }
      Close FIC;
    mais j'ai comme erreur (fichier non trouvé)
    svp regarder si vous voyez des fautes à corriger que j'ai pas vu
    d'avance merci

  2. #2
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    my $path = "./xls_txt.txt";
    die "fichier non trouve !\n" if (! -s $path);

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    merci pour la réponse
    mais sincèrement je vois pas la différence dans ce que vous avez corrigé
    je l'ai pourtant essayé au cas ou je vois pas bien, mais j'ai toujours la même erreur
    merci de votre aide.
    regarder s'il ya une faute

  4. #4
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    my $path = "./xls_txt.txt";
    die "fichier non trouve !\n" if (! -s $path);
    Cette ligne veut dire : Arrete le script et met moi le message fichier non trouve si ce dernier n'existe pas ou à une taille égal à 0.
    Or je suppute que vous cherchez à le créer de novo, ainsi, au premier lancement du script, ce dernier ne sera jamais crée, vu qu'il n'existe pas .
    Donc étant débutant, faut éviter les copier coller à tout va et essayer soit même pas à pas, quite à ce faire guider par le forum, ce serait mieux pour l'apprentissage de perl

  5. #5
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Essayé plutot ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    my $fichier_txt = "./xls_txt.txt";
    my $fichier_xls = "./20070611 cheklist_massy.xls";
    die "fichier non trouve !\n" if (! -s $fichier_xls);
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
    et non
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    my $path = "./xls_txt.txt";
    die "fichier non trouve !\n" if (! -s $path);
    open(FIC, ">>$path" ) or die "Can't open file: $!";
    et remplacer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Spreadsheet::ParseExcel::Workbook->Parse('./20070611 cheklist_massy.xls');
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Spreadsheet::ParseExcel::Workbook->Parse($fichier_xls);

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    merci de votre réponse
    j'ai fais ce que vous m'avez conseiller, mais j'ai tjs la même erreur (fichier non trouve !)
    voila mon code rechangé
    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
    #!c:/perl/bin/perl -w
    use strict;
     
    use warnings;
    use Spreadsheet::ParseExcel;
    my @lignes=0;
     
    my $fichier_txt = "./xls_txt.txt";
    my $fichier_xls = "./20070611 cheklist_massy.xls";
    die "fichier non trouve !\n" if (! -s $fichier_xls);
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
     
    @lignes = <FIC>;
    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($fichier_xls);
    my($iR, $iC, $oWkS, $oWkC);
    foreach my $oWkS (@{$oBook->{Worksheet}}) {
       print "--------- SHEET:", $oWkS->{Name}, "\n";
      for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
      $iR++) {
         for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
     $iC++) {
          $oWkC = $oWkS->{Cells}[$iR][$iC];
        print FIC "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
        }
      }
      }
      Close FIC;
    merci de m'aider

  7. #7
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    my $fichier_xls = "./20070611 cheklist_massy.xls";
    est ce que ce fichier existe ./20070611 cheklist_massy.xls ???????????
    De plus l'espace entre ./20070611 et cheklist_massy.xls
    ??

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    effectivement j'ai changer le nom du fichier source. et j'ai lancé mon script
    j'ai eu un résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    C:\Program Files\EasyPHP1-8\www\traitement\trtaxe10>xls_txt.pl
    Filehandle FIC opened only for output at C:\Program Files\EasyPHP1-8\www\traitem
    ent\trtaxe10\xls_txt.pl line 13.
    --------- SHEET:Feuil1
    Can't locate object method "Close" via package "IO::Handle" at C:\Program Files\
    EasyPHP1-8\www\traitement\trtaxe10\xls_txt.pl line 27.
    mais j'ai récupéré le contenu de fichier xls dans le fichier .txt
    or ce que j'ai récupéré dans mon fichier txt , les lignes du fichier xls en colonnes dans mon fichier txt
    est-il possible de récupérer les lignes de xls en ligne dans .txt
    voila en joint .xls et .txt
    Fichiers attachés Fichiers attachés

  9. #9
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    qd je met ce que vous m'avez dis le script ne marche pas et j'ai comme retour
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    C:\Program Files\EasyPHP1-8\www\traitement\trtaxe10>xls_txt.pl
    Bareword "FIC" not allowed while "strict subs" in use at C:\Program Files\EasyPH
    P1-8\www\traitement\trtaxe10\xls_txt.pl line 27.
    Execution of C:\Program Files\EasyPHP1-8\www\traitement\trtaxe10\xls_txt.pl abor
    ted due to compilation errors.

  11. #11
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    bon, mais nous ton script en entier.

  12. #12
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    voila mon script en entier.
    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
    #!c:/perl/bin/perl -w
    use strict;
     
    use warnings;
    use Spreadsheet::ParseExcel;
    my @lignes=0;
     
    my $fichier_txt = "./xls_txt.txt";
    my $fichier_xls = "./srctst.xls";
    die "fichier non trouve !\n" if (! -s $fichier_xls);
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
     
    @lignes = <FIC>;
    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($fichier_xls);
    my($iR, $iC, $oWkS, $oWkC);
    foreach my $oWkS (@{$oBook->{Worksheet}}) {
       print "--------- SHEET:", $oWkS->{Name}, "\n";
      for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
      $iR++) {
         for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
     $iC++) {
          $oWkC = $oWkS->{Cells}[$iR][$iC];
        print FIC "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
        }
      }
      }
      Close FIC; # avec Close(FIC)  ça ne marche pas
    merci beaucoup pour votre aide.

  13. #13
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    je te demandais de mettre avec un c en minuscule

  14. #14
    Membre chevronné
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2003
    Messages
    1 572
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2003
    Messages : 1 572
    Points : 2 014
    Points
    2 014
    Par défaut
    Ton close à la fin n'est pas bon : Perl est sensible à la casse !!!

    Donc, c'est pas Close mais close

  15. #15
    Membre chevronné
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2003
    Messages
    1 572
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2003
    Messages : 1 572
    Points : 2 014
    Points
    2 014
    Par défaut
    Zut, grilled by Djibril

  16. #16
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    bon tu te melange beaucoup les pedales.
    1)
    On ne declare pas un tableau ainsi. c'est
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    my @lignes; ou my @lignes =();
    2)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
    Là, tu fais une creation de fichier en ecriture. Et à ce fichier est associé un filehandle nommée FIC.
    n'a plus aucun sens car il sert à mettre un fichier dans un tableau.
    Donc vire Donc voilà ton script :

    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
     
    #!c:/perl/bin/perl
    use strict;
    use warnings;
    use Spreadsheet::ParseExcel;
    my @lignes;
     
    my $fichier_txt = "./xls_txt.txt";
    my $fichier_xls = "./srctst.xls";
    die "fichier non trouve !\n" if (! -s $fichier_xls);
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
     
    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($fichier_xls);
    my($iR, $iC, $oWkS, $oWkC);
    foreach my $oWkS (@{$oBook->{Worksheet}}) {
       print "--------- SHEET:", $oWkS->{Name}, "\n";
      for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
      $iR++) {
         for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
     $iC++) {
          $oWkC = $oWkS->{Cells}[$iR][$iC];
        print FIC "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
        }
      }
      }
      close FIC;

  17. #17
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    d'accord comme il l'as dis Arioch c vrais tant pie pour moi
    je m'excuse ça marche mnt
    mais un autre souci moi je voudrais parser ce fichier mais le prm est que je récupère les ligne de xls en colonne dans txt
    est -il possible d'avoir la même présentation dans txt que dans xls?

  18. #18
    Membre chevronné
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2003
    Messages
    1 572
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2003
    Messages : 1 572
    Points : 2 014
    Points
    2 014
    Par défaut
    Essaie ceci pour voir (remplace la partie correspondante dans ton script bien sûr) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
         for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) {
          $oWkC = $oWkS->{Cells}[$iR][$iC];
        print FIC $oWkC->Value, "\t";
        }
        print FIC "\n";
      }

  19. #19
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    296
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 296
    Points : 73
    Points
    73
    Par défaut
    merci d'avance.
    avec le nouveau changement voila mon script
    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
    #!c:/perl/bin/perl -w
    use strict;
     
    use warnings;
    use Spreadsheet::ParseExcel;
    my @lignes=0;
     
    my $fichier_txt = "./xls_txt.txt";
    my $fichier_xls = "./srctst.xls";
    die "fichier non trouve !\n" if (! -s $fichier_xls);
    open(FIC, ">$fichier_txt" ) or die "impossible d'ecrire dans le fichier $fichier_txt : $!";
     
    @lignes = <FIC>;
    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($fichier_xls);
    my($iR, $iC, $oWkS, $oWkC);
    foreach my $oWkS (@{$oBook->{Worksheet}}) {
       print "--------- SHEET:", $oWkS->{Name}, "\n";
     
    for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {  
    	for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) {  
    		$oWkC = $oWkS->{Cells}[$iR][$iC]; 
    		print FIC $oWkC->Value, "\t"; 
    	}
    	print FIC "\n"; 
    	}
     
      }
      close (FIC); # avec Close(FIC)  ça ne marche pas
    et voila ce que j'ai comme retour
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    C:\Program Files\EasyPHP1-8\www\traitement\trtaxe10>xls_txt.pl
    Filehandle FIC opened only for output at C:\Program Files\EasyPHP1-8\www\traitem
    ent\trtaxe10\xls_txt.pl line 13.
    --------- SHEET:Feuil1
    Can't call method "Value" on an undefined value at C:\Program Files\EasyPHP1-8\w
    ww\traitement\trtaxe10\xls_txt.pl line 22.
    et dans le fichier txt j'ai eu seulement la première ligne ou il y a seulement une phrase

  20. #20
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    enleve cette ligne
    Lis la FAQ pour apprendre la base.

    Sinon pour ton script remplace
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    print FIC $oWkC->Value, "\t";
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    print FIC $oWkC->Value if($oWkC);
    Il s'arrete en ligne 1 car les cellules sont vides d'ou le message d'errreur
    Can't call method "Value" on an undefined value at C:\Program Files\EasyPHP1-8\w
    ww\traitement\trtaxe10\xls_txt.pl line 22.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [XL-2007] Copie d'onglet avec des bouton dans un fichier .xls
    Par xdupart dans le forum Excel
    Réponses: 4
    Dernier message: 12/03/2015, 10h07
  2. Copie de fichiers XLS dans une table
    Par sebvita dans le forum Oracle
    Réponses: 3
    Dernier message: 28/12/2005, 09h13
  3. copier le contenu d'une page web dans un fichier texte
    Par wassila dans le forum C++Builder
    Réponses: 30
    Dernier message: 28/08/2005, 22h27
  4. Afficher le contenu d'un fichier xls dans un DBgrid
    Par bianconeri dans le forum C++Builder
    Réponses: 5
    Dernier message: 03/09/2004, 16h35
  5. Réponses: 2
    Dernier message: 16/07/2004, 09h30

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