PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV (précédentes versions) → extraction de données à partir d'un classeur excel
extraction de données à partir d'un classeur excel
Débuté par MEITE MAMADOU, 06 juil. 2020 13:06 - 3 réponses
Posté le 06 juillet 2020 - 13:06
Bonjour à tous.

j'ai besoin d'un coup de main.

je développe un petite solution pour extraire des données à partir d'un classeur excel et ensuite y extraire des possibles doublons.
Tout mon process de tri marche mais lorsque je termine un classeur excel et que je fais le même traitement avec le 2e classeur, les données du 1er classeur sont effacées de ma table. ci joint le code. Merci d'avance pour vos aides.
//Importation des données du fichiers excel
//declaration des variables
IdFichierXLS, Idfilepropre sont des entiers
FichierExcel, FichierExcelPropre, CodConcaten, CheminDeFichier sont des chaînes
NbLigne,NbLignepr, NbColonne,NbColonnepr sont des entiers
CheminDeFichier=FEN_Apurement.SAI_chemin
FichierExcel= CheminDeFichier
FichierExcelPropre="C:\Users\MMEITE\Desktop\Apurement\DR odienne propre.XLSX"

SI fFichierExiste(FichierExcel) ET fFichierExiste(FichierExcelPropre) ALORS
HCréation(List_ZCZDILOT)
HCréation(ZCZDILOT_Propre)

IdFichierXLS = xlsOuvre(CheminDeFichier)
Idfilepropre= xlsOuvre("C:\Users\MMEITE\Desktop\Apurement\DR odienne propre.XLSX")
NbLigne=xlsNbLigne(IdFichierXLS)
NbColonne=xlsNbColonne(IdFichierXLS)
NbLignepr=xlsNbLigne(Idfilepropre)
NbColonnepr=xlsNbColonne(Idfilepropre)

HOuvre(List_ZCZDILOT)
HOuvre(ZCZDILOT_Propre)

// SI IdFichierXLS = 0 ALORS
// Info(" Le fichier Excel ne contient aucun enregistrement")
// RETOUR
// FIN

POUR i=2 A NbLigne
CodConcaten=""
SI IdFichierXLS <> -1 ALORS

//recuperation des # codes pour créer la clé concatenée
CodConcaten=xlsDonnée (IdFichierXLS,i,1,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,3,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,5,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,7,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,9,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,11,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,13,Vrai)

//affectation des valeurs de colonnes dans les champs concernés
List_ZCZDILOT.CodReg=xlsDonnée (IdFichierXLS,i,1,Vrai)
List_ZCZDILOT.LibReg=xlsDonnée (IdFichierXLS,i,2,Vrai)
List_ZCZDILOT.CodDep=xlsDonnée (IdFichierXLS,i,3,Vrai)
List_ZCZDILOT.NomDep=xlsDonnée (IdFichierXLS,i,4,Vrai)
List_ZCZDILOT.CodSp=xlsDonnée (IdFichierXLS,i,5,Vrai)
List_ZCZDILOT.NomSp=xlsDonnée (IdFichierXLS,i,6,Vrai)
List_ZCZDILOT.CodCom=xlsDonnée (IdFichierXLS,i,7,Vrai)
List_ZCZDILOT.NomCom=xlsDonnée (IdFichierXLS,i,8,Vrai)
List_ZCZDILOT.CodLoc=xlsDonnée (IdFichierXLS,i,9,Vrai)
List_ZCZDILOT.NomLoc=xlsDonnée (IdFichierXLS,i,10,Vrai)
List_ZCZDILOT.CodQtierCpt=xlsDonnée (IdFichierXLS,i,11,Vrai)
List_ZCZDILOT.NomQtierCpt=xlsDonnée (IdFichierXLS,i,12,Vrai)
List_ZCZDILOT.NumIlotCpt=xlsDonnée (IdFichierXLS,i,13,Vrai)
List_ZCZDILOT.TotHabitant=xlsDonnée (IdFichierXLS,i,14,Vrai)
List_ZCZDILOT.PopZD=xlsDonnée (IdFichierXLS,i,15,Vrai)
List_ZCZDILOT.NumZD=xlsDonnée (IdFichierXLS,i,16,Vrai)
List_ZCZDILOT.NumZC=xlsDonnée (IdFichierXLS,i,17,Vrai)
List_ZCZDILOT.CodConcatenation=CodConcaten
List_ZCZDILOT.Enregistr_Date=DateSys()
List_ZCZDILOT.Enregistr_Heure=HeureSys()
List_ZCZDILOT.NOmDeFichierExcel=SAI_chemin
List_ZCZDILOT.NumLignExcel=i

Doublons_ZCZDILOT.NumIlotCpt=xlsDonnée (IdFichierXLS,i,13,Vrai)

// Champ jauge
FEN_Apurement..JaugeSystème = JAUGE_Jauge1..Nom
JAUGE_Jauge1..Libellé="Extraction des données"
JAUGE_Jauge1..BorneMin = 0
JAUGE_Jauge1..BorneMax = NbLigne
JAUGE_Jauge1..Valeur = i
Multitâche()

HAjoute(List_ZCZDILOT)
TableAffiche(TABLE_List_ZCZDILOT)
FIN
FIN

SINON
Erreur("Le fichier à apurer n'existe pas")
RETOUR
FIN
nb est un entier=TABLE_List_ZCZDILOT..Occurrence
LIB_Liste= nb+" ligne(s)"
FEN_Apurement.JAUGE_Jauge1..Visible=Faux
MonCod est une chaîne
NBredelignzd est un entier=HNbEnr(List_ZCZDILOT)

// debut du tri pour separer les doublons et les données propres
HLitPremier(List_ZCZDILOT)
TANTQUE PAS HEnDehors(List_ZCZDILOT)
MonCod = List_ZCZDILOT.CodConcatenation

HLitRecherche(ZCZDILOT_Propre,CodConcatenation,MonCod)
SI PAS HTrouve(ZCZDILOT_Propre) ALORS
ZCZDILOT_Propre.CodReg=List_ZCZDILOT.CodReg
ZCZDILOT_Propre.LibReg=List_ZCZDILOT.LibReg
ZCZDILOT_Propre.CodDep=List_ZCZDILOT.CodDep
ZCZDILOT_Propre.NomDep=List_ZCZDILOT.NomDep
ZCZDILOT_Propre.CodSp=List_ZCZDILOT.CodSp
ZCZDILOT_Propre.NomSp=List_ZCZDILOT.NomSp
ZCZDILOT_Propre.CodCom=List_ZCZDILOT.CodCom
ZCZDILOT_Propre.NomCom=List_ZCZDILOT.NomCom
ZCZDILOT_Propre.CodLoc=List_ZCZDILOT.CodLoc
ZCZDILOT_Propre.NomLoc=List_ZCZDILOT.NomLoc
ZCZDILOT_Propre.CodQtierCpt=List_ZCZDILOT.CodQtierCpt
ZCZDILOT_Propre.NomQtierCpt=List_ZCZDILOT.NomQtierCpt
ZCZDILOT_Propre.NumIlotCpt=List_ZCZDILOT.NumIlotCpt
ZCZDILOT_Propre.TotHabitant=List_ZCZDILOT.TotHabitant
ZCZDILOT_Propre.PopZD=List_ZCZDILOT.PopZD
ZCZDILOT_Propre.NumZD=List_ZCZDILOT.NumZD
ZCZDILOT_Propre.NumZC=List_ZCZDILOT.NumZC
ZCZDILOT_Propre.CodConcatenation=List_ZCZDILOT.CodConcatenation
ZCZDILOT_Propre.Enregistr_Date=DateSys()
ZCZDILOT_Propre.Enregistr_Heure=HeureSys()
ZCZDILOT_Propre.NOmDeFichierExcel=List_ZCZDILOT.NOmDeFichierExcel
ZCZDILOT_Propre.NumLignExcel=List_ZCZDILOT.NumLignExcel

HAjoute(ZCZDILOT_Propre)
TableAffiche(TABLE_propre_ZCZDILOT)
SINON

Doublons_ZCZDILOT.CodReg=List_ZCZDILOT.CodReg
Doublons_ZCZDILOT.LibReg=List_ZCZDILOT.LibReg
Doublons_ZCZDILOT.CodDep=List_ZCZDILOT.CodDep
Doublons_ZCZDILOT.NomDep=List_ZCZDILOT.NomDep
Doublons_ZCZDILOT.CodSp=List_ZCZDILOT.CodSp
Doublons_ZCZDILOT.NomSp=List_ZCZDILOT.NomSp
Doublons_ZCZDILOT.CodCom=List_ZCZDILOT.CodCom
Doublons_ZCZDILOT.NomCom=List_ZCZDILOT.NomCom
Doublons_ZCZDILOT.CodLoc=List_ZCZDILOT.CodLoc
Doublons_ZCZDILOT.NomLoc=List_ZCZDILOT.NomLoc
Doublons_ZCZDILOT.CodQtierCpt=List_ZCZDILOT.CodQtierCpt
Doublons_ZCZDILOT.NomQtierCpt=List_ZCZDILOT.NomQtierCpt
Doublons_ZCZDILOT.NumIlotCpt=List_ZCZDILOT.NumIlotCpt
Doublons_ZCZDILOT.TotHabitant=List_ZCZDILOT.TotHabitant
Doublons_ZCZDILOT.PopZD=List_ZCZDILOT.PopZD
Doublons_ZCZDILOT.NumZD=List_ZCZDILOT.NumZD
Doublons_ZCZDILOT.NumZC=List_ZCZDILOT.NumZC
Doublons_ZCZDILOT.CodConcatenation=List_ZCZDILOT.CodConcatenation
Doublons_ZCZDILOT.Enregistr_Date=DateSys()
Doublons_ZCZDILOT.Enregistr_Heure=HeureSys()
Doublons_ZCZDILOT.NOmDeFichierExcel=List_ZCZDILOT.NOmDeFichierExcel
Doublons_ZCZDILOT.NumLignExcel=List_ZCZDILOT.NumLignExcel

HAjoute(Doublons_ZCZDILOT)
TableAffiche(TABLE_Doublon)
FIN

HLitSuivant(List_ZCZDILOT)

// Champ jauge
FEN_Apurement..JaugeSystème = JAUGE_filtrage..Nom
JAUGE_filtrage..Libellé="suppression des doublons"
JAUGE_filtrage..BorneMin = 0
JAUGE_filtrage..BorneMax = NBredelignzd
JAUGE_filtrage..Valeur = i
Multitâche()
FIN
Info("terminé!!")

ZB1, ZB2 sont des entiers
ZB1=TABLE_propre_ZCZDILOT..Occurrence
ZB2=TABLE_Doublon..Occurrence

LIB_propr= ZB1+" ligne(s)"
LIB_doublon=ZB2 +" ligne(s)"
JAUGE_filtrage..Visible=Faux
JAUGE_Jauge1..Visible=Faux
Membre enregistré
3 881 messages
Popularité : +227 (347 votes)
Posté le 07 juillet 2020 - 10:11
Bonjour,
Pourquoi ne pas utiliser HImporteXLS avec l'option hImpTestDoublon ?
Attention toutefois à contrôler les autres valeurs en cas de doublon. En effet, il est possible que des lignes aient le même ID mais que les valeurs aient changé entre l'enregistrement des deux classeurs. Il faut prendre ça en compte dans ton :
CAS ERREUR
SI HErreurDoublon
//Traitement Manuel
FIN


--
Il y a peut être plus simple, mais, ça tourne
Posté le 10 juillet 2020 - 16:17
Merci beaucoup pour ta réponse.
en fait, ma recherche de doublon se passe bien. Mon problème se situe au niveau de la conservation des données dans ma base hfsql
Membre enregistré
2 569 messages
Popularité : +222 (260 votes)
Posté le 10 juillet 2020 - 17:33
Bonjour,

Il manque les HRAZ(). Ca permet de vider intégralement le buffer des données précédentes et d'indiquer à Windev que c'est un nouvel enregistrement.

Ca donnerait ceci
//Importation des données du fichiers excel
//declaration des variables
IdFichierXLS, Idfilepropre sont des entiers
FichierExcel, FichierExcelPropre, CodConcaten, CheminDeFichier sont des chaînes
NbLigne,NbLignepr, NbColonne,NbColonnepr sont des entiers
CheminDeFichier=FEN_Apurement.SAI_chemin
FichierExcel= CheminDeFichier
FichierExcelPropre="C:\Users\MMEITE\Desktop\Apurement\DR odienne propre.XLSX"

SI fFichierExiste(FichierExcel) ET fFichierExiste(FichierExcelPropre) ALORS
HCréation(List_ZCZDILOT)
HCréation(ZCZDILOT_Propre)

IdFichierXLS = xlsOuvre(CheminDeFichier)
Idfilepropre= xlsOuvre("C:\Users\MMEITE\Desktop\Apurement\DR odienne propre.XLSX")
NbLigne=xlsNbLigne(IdFichierXLS)
NbColonne=xlsNbColonne(IdFichierXLS)
NbLignepr=xlsNbLigne(Idfilepropre)
NbColonnepr=xlsNbColonne(Idfilepropre)

HOuvre(List_ZCZDILOT)
HOuvre(ZCZDILOT_Propre)

// SI IdFichierXLS = 0 ALORS
// Info(" Le fichier Excel ne contient aucun enregistrement")
// RETOUR
// FIN

POUR i=2 À NbLigne
CodConcaten=""
SI IdFichierXLS <> -1 ALORS

//recuperation des # codes pour créer la clé concatenée
CodConcaten=xlsDonnée (IdFichierXLS,i,1,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,3,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,5,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,7,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,9,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,11,Vrai)
CodConcaten=CodConcaten+xlsDonnée (IdFichierXLS,i,13,Vrai)

//affectation des valeurs de colonnes dans les champs concernés
HRAZ(List_ZCZDILOT)
List_ZCZDILOT.CodReg=xlsDonnée (IdFichierXLS,i,1,Vrai)
List_ZCZDILOT.LibReg=xlsDonnée (IdFichierXLS,i,2,Vrai)
List_ZCZDILOT.CodDep=xlsDonnée (IdFichierXLS,i,3,Vrai)
List_ZCZDILOT.NomDep=xlsDonnée (IdFichierXLS,i,4,Vrai)
List_ZCZDILOT.CodSp=xlsDonnée (IdFichierXLS,i,5,Vrai)
List_ZCZDILOT.NomSp=xlsDonnée (IdFichierXLS,i,6,Vrai)
List_ZCZDILOT.CodCom=xlsDonnée (IdFichierXLS,i,7,Vrai)
List_ZCZDILOT.NomCom=xlsDonnée (IdFichierXLS,i,8,Vrai)
List_ZCZDILOT.CodLoc=xlsDonnée (IdFichierXLS,i,9,Vrai)
List_ZCZDILOT.NomLoc=xlsDonnée (IdFichierXLS,i,10,Vrai)
List_ZCZDILOT.CodQtierCpt=xlsDonnée (IdFichierXLS,i,11,Vrai)
List_ZCZDILOT.NomQtierCpt=xlsDonnée (IdFichierXLS,i,12,Vrai)
List_ZCZDILOT.NumIlotCpt=xlsDonnée (IdFichierXLS,i,13,Vrai)
List_ZCZDILOT.TotHabitant=xlsDonnée (IdFichierXLS,i,14,Vrai)
List_ZCZDILOT.PopZD=xlsDonnée (IdFichierXLS,i,15,Vrai)
List_ZCZDILOT.NumZD=xlsDonnée (IdFichierXLS,i,16,Vrai)
List_ZCZDILOT.NumZC=xlsDonnée (IdFichierXLS,i,17,Vrai)
List_ZCZDILOT.CodConcatenation=CodConcaten
List_ZCZDILOT.Enregistr_Date=DateSys()
List_ZCZDILOT.Enregistr_Heure=HeureSys()
List_ZCZDILOT.NOmDeFichierExcel=SAI_chemin
List_ZCZDILOT.NumLignExcel=i

Doublons_ZCZDILOT.NumIlotCpt=xlsDonnée (IdFichierXLS,i,13,Vrai)

// Champ jauge
FEN_Apurement..JaugeSystème = JAUGE_Jauge1..Nom
JAUGE_Jauge1..Libellé="Extraction des données"
JAUGE_Jauge1..BorneMin = 0
JAUGE_Jauge1..BorneMax = NbLigne
JAUGE_Jauge1..Valeur = i
Multitâche()

HAjoute(List_ZCZDILOT)
TableAffiche(TABLE_List_ZCZDILOT)
FIN
FIN

SINON
Erreur("Le fichier à apurer n'existe pas")
RETOUR
FIN
nb est un entier=TABLE_List_ZCZDILOT..Occurrence
LIB_Liste= nb+" ligne(s)"
FEN_Apurement.JAUGE_Jauge1..Visible=Faux
MonCod est une chaîne
nNBredelignzd est un entier=HNbEnr(List_ZCZDILOT)

// debut du tri pour separer les doublons et les données propres
HLitPremier(List_ZCZDILOT)
TANTQUE PAS HEnDehors(List_ZCZDILOT)
MonCod = List_ZCZDILOT.CodConcatenation

HLitRecherche(ZCZDILOT_Propre,CodConcatenation,MonCod)
SI PAS HTrouve(ZCZDILOT_Propre) ALORS
HRAZ(ZCZDILOT_Propre)
ZCZDILOT_Propre.CodReg=List_ZCZDILOT.CodReg
ZCZDILOT_Propre.LibReg=List_ZCZDILOT.LibReg
ZCZDILOT_Propre.CodDep=List_ZCZDILOT.CodDep
ZCZDILOT_Propre.NomDep=List_ZCZDILOT.NomDep
ZCZDILOT_Propre.CodSp=List_ZCZDILOT.CodSp
ZCZDILOT_Propre.NomSp=List_ZCZDILOT.NomSp
ZCZDILOT_Propre.CodCom=List_ZCZDILOT.CodCom
ZCZDILOT_Propre.NomCom=List_ZCZDILOT.NomCom
ZCZDILOT_Propre.CodLoc=List_ZCZDILOT.CodLoc
ZCZDILOT_Propre.NomLoc=List_ZCZDILOT.NomLoc
ZCZDILOT_Propre.CodQtierCpt=List_ZCZDILOT.CodQtierCpt
ZCZDILOT_Propre.NomQtierCpt=List_ZCZDILOT.NomQtierCpt
ZCZDILOT_Propre.NumIlotCpt=List_ZCZDILOT.NumIlotCpt
ZCZDILOT_Propre.TotHabitant=List_ZCZDILOT.TotHabitant
ZCZDILOT_Propre.PopZD=List_ZCZDILOT.PopZD
ZCZDILOT_Propre.NumZD=List_ZCZDILOT.NumZD
ZCZDILOT_Propre.NumZC=List_ZCZDILOT.NumZC
ZCZDILOT_Propre.CodConcatenation=List_ZCZDILOT.CodConcatenation
ZCZDILOT_Propre.Enregistr_Date=DateSys()
ZCZDILOT_Propre.Enregistr_Heure=HeureSys()
ZCZDILOT_Propre.NOmDeFichierExcel=List_ZCZDILOT.NOmDeFichierExcel
ZCZDILOT_Propre.NumLignExcel=List_ZCZDILOT.NumLignExcel

HAjoute(ZCZDILOT_Propre)
TableAffiche(TABLE_propre_ZCZDILOT)
SINON
HRAZ(Doublons_ZCZDILOT)
Doublons_ZCZDILOT.CodReg=List_ZCZDILOT.CodReg
Doublons_ZCZDILOT.LibReg=List_ZCZDILOT.LibReg
Doublons_ZCZDILOT.CodDep=List_ZCZDILOT.CodDep
Doublons_ZCZDILOT.NomDep=List_ZCZDILOT.NomDep
Doublons_ZCZDILOT.CodSp=List_ZCZDILOT.CodSp
Doublons_ZCZDILOT.NomSp=List_ZCZDILOT.NomSp
Doublons_ZCZDILOT.CodCom=List_ZCZDILOT.CodCom
Doublons_ZCZDILOT.NomCom=List_ZCZDILOT.NomCom
Doublons_ZCZDILOT.CodLoc=List_ZCZDILOT.CodLoc
Doublons_ZCZDILOT.NomLoc=List_ZCZDILOT.NomLoc
Doublons_ZCZDILOT.CodQtierCpt=List_ZCZDILOT.CodQtierCpt
Doublons_ZCZDILOT.NomQtierCpt=List_ZCZDILOT.NomQtierCpt
Doublons_ZCZDILOT.NumIlotCpt=List_ZCZDILOT.NumIlotCpt
Doublons_ZCZDILOT.TotHabitant=List_ZCZDILOT.TotHabitant
Doublons_ZCZDILOT.PopZD=List_ZCZDILOT.PopZD
Doublons_ZCZDILOT.NumZD=List_ZCZDILOT.NumZD
Doublons_ZCZDILOT.NumZC=List_ZCZDILOT.NumZC
Doublons_ZCZDILOT.CodConcatenation=List_ZCZDILOT.CodConcatenation
Doublons_ZCZDILOT.Enregistr_Date=DateSys()
Doublons_ZCZDILOT.Enregistr_Heure=HeureSys()
Doublons_ZCZDILOT.NOmDeFichierExcel=List_ZCZDILOT.NOmDeFichierExcel
Doublons_ZCZDILOT.NumLignExcel=List_ZCZDILOT.NumLignExcel

HAjoute(Doublons_ZCZDILOT)
TableAffiche(TABLE_Doublon)
FIN

HLitSuivant(List_ZCZDILOT)

// Champ jauge
FEN_Apurement..JaugeSystème = JAUGE_filtrage..Nom
JAUGE_filtrage..Libellé="suppression des doublons"
JAUGE_filtrage..BorneMin = 0
JAUGE_filtrage..BorneMax = NBredelignzd
JAUGE_filtrage..Valeur = i
Multitâche()
FIN
Info("terminé!!")

ZB1, ZB2 sont des entiers
ZB1=TABLE_propre_ZCZDILOT..Occurrence
ZB2=TABLE_Doublon..Occurrence

LIB_propr= ZB1+" ligne(s)"
LIB_doublon=ZB2 +" ligne(s)"
JAUGE_filtrage..Visible=Faux
JAUGE_Jauge1..Visible=Faux


--
Cordialement,

Philippe SAINT-BERTIN