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

Scripts/Batch Discussion :

copier plusieurs csv dans un excel en powershell


Sujet :

Scripts/Batch

  1. #1
    Membre éclairé
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2014
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2014
    Messages : 326
    Par défaut copier plusieurs csv dans un excel en powershell
    Bonjour,

    Je voudrais créer un fichier excel qui comprendrai plusieurs fichiers csv

    L'idée est la suivante j'ai 3 fichiers
    - Liste_globale.csv
    - Liste_actifs.csv
    - liste_inactif.csv

    j'aimerai pouvoir créer un fichier xlsx (Personnel_YYYY-MM.xlsx) qui intégrerai les données dans des feuilles différentes
    - Liste_globale.csv dans la feuille "Totalité"
    - Liste_actifs.csv dans la feuille "actif"
    - liste_inactif.csv dans la feuille "hors"

    Tous les fichiers csv sont de meme format mais pas obligatoirement dans le meme répertoire.



    Code POWERSHELL : 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
    $DateJ ="2021-10"
    $file = $path2 +"\Global_test2.xls"
    $EMPLOYEE_GLOBAL =$path2+"\DB-EMPLOYEE-GLOBAL_"+$DateJ+".csv"
    $EMPLOYEE_GLOBAL_ACTIVE =$path3+"\DB-EMPLOYEE-GLOBAL_ACTIVE_"+$DateJ+".csv"
    $EMPLOYEE_GLOBAL_INACTIVE =$path4+"\DB-EMPLOYEE-GLOBAL_INACTIVE_"+$DateJ+".csv"
    $wb1 = $excel.workbooks.open($EMPLOYEE_GLOBAL, $null, $true) #open source, readonly
    $wb2 = $excel.workbooks.open($file) #open target
    #Recherche dans le dossier avec les fichiers Excel
    $dossier = $path2
    $dossiers = Get-ChildItem $dossier  
    #Création d'un fichier EXCEL
    $file = new-item $filetype file -force
    #Lancer Excel
    $excel = new-object -c Excel.Application
    $excel.visible = $false
    $excel.DisplayAlerts = $False
    $wb2.Worksheets.item().add.name ="TOTALITE"
    $wb2.Worksheets.item().add.name ="ACTIF"
    $wb2.Worksheets.item().add.name ="HORS"

    Je bloque déja sur le création des feuilles.

    Merci pour votre aide

  2. #2
    Membre éclairé
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2014
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2014
    Messages : 326
    Par défaut
    J'ai modifié le code

    Code POWERSHELL : 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
    $DateJ ="2021-10"
    $file = $path2 +"\Global_test2.xlsx"
    $EMPLOYEE_GLOBAL =$path2+"\DB-EMPLOYEE-GLOBAL_"+$DateJ+".csv"
    $EMPLOYEE_GLOBAL_ACTIVE =$path2+"\DB-EMPLOYEE-GLOBAL_ACTIVE_"+$DateJ+".csv"
    $EMPLOYEE_GLOBAL_INACTIVE =$path2+"\DB-EMPLOYEE-GLOBAL_INACTIVE_"+$DateJ+".csv"
    $wb1 = $excel.workbooks.open($EMPLOYEE_GLOBAL, $null, $true) #open source, readonly
    $wb2 = $excel.workbooks.open($file) #open target
    #Recherche dans le dossier avec les fichiers Excel
    $dossier = $path2
    #Création d'un fichier EXCEL
    $file = new-item $filetype file -force
    #Lancer Excel
    $excel = new-object -c Excel.Application
    $excel.visible = $false
    $excel.DisplayAlerts = $False
     
    $sheet1 = $wb2.worksheets.Item(1)
    $sheet1.name = "TOTALITE"
    $wb2.worksheets.add()
    $sheet2 = $wb2.worksheets.Item(2)
    $sheet2.name = "ACTIF"
    $wb3.worksheets.add()
    $sheet3 = $wb2.worksheets.Item(2)
    $sheet3.name = "HORS"
     
    $wb2.close($true) # close and save destination workbook

    Message d'erreur que j'obtiens

    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
    Impossible d'ouvrir le fichier «Global_test2.xlsx» car son format ou son extension n'est pas valide. Vérifiez que le fichier n'est pas endommagé et que son extension correspond bien au format du 
    fichier.
    Au caractère Ligne:7 : 1
    + $wb2 = $excel.workbooks.open($file) #open target
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:17 : 1
    + $sheet1 = $wb2.worksheets.Item(1)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Exception de HRESULT : 0x800A01A8
    Au caractère Ligne:18 : 1
    + $sheet1.name = "TOTALITE"
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:19 : 1
    + $wb2.worksheets.add()
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:20 : 1
    + $sheet2 = $wb2.worksheets.Item(2)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Exception de HRESULT : 0x800A01A8
    Au caractère Ligne:21 : 1
    + $sheet2.name = "ACTIF"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:22 : 1
    + $wb3.worksheets.add()
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:23 : 1
    + $sheet3 = $wb2.worksheets.Item(2)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Exception de HRESULT : 0x800A01A8
    Au caractère Ligne:24 : 1
    + $sheet3.name = "HORS"
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    L’objet invoqué s’est déconnecté de ses clients. (Exception de HRESULT : 0x80010108 (RPC_E_DISCONNECTED))
    Au caractère Ligne:26 : 1
    + $wb2.close($true) # close and save destination workbook
    + ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

  3. #3
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Voici un exemple simple

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $False
    $excel.DisplayAlerts = $False
     
    $workbook = $excel.Workbooks.Add()
     
    $workbook.Worksheets(1).Name = "TOTALITE"
    $workbook.Worksheets(2).Name = "ACTIF"
    $workbook.Worksheets(3).Name = "HORS"
     
    # Remplir les données des 3 feuilles
     
    $workbook.SaveAs([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) + "\MonFichierExcelTest.xlsx")
    $workbook.Close()
    $excel.Quit()

  4. #4
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Voici pour l'import dans la première feuille du fichier MonFichierExcelTest.csv

    Code powershell : 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
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $True
    $excel.DisplayAlerts = $False
     
    $workbook = $excel.Workbooks.Add()
     
    $workbook.Worksheets(1).Name = "TOTALITE"
    $workbook.Worksheets(2).Name = "ACTIF"
    $workbook.Worksheets(3).Name = "HORS"
     
    # Build the QueryTables.Add command and reformat the data
    $worksheet = $workbook.Worksheets(1)
    $TxtConnector = ("TEXT;MonFichierExcelTest.csv")
    $Connector = $worksheet.QueryTables.add($TxtConnector, $worksheet.Range("A1"))
    $query = $worksheet.QueryTables.item($Connector.name)
    $query.TextFileOtherDelimiter = ";"
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1
     
    # Execute & delete the import query
    $query.Refresh()
    $query.Delete()
     
    $workbook.SaveAs([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) + "\MonFichierExcelTest.xlsx")
    $workbook.Close()
    $excel.Quit()

    https://code.adonline.id.au/csv-to-xlsx-powershell/

  5. #5
    Membre éclairé
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2014
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2014
    Messages : 326
    Par défaut
    Merci

    mais j'ai des messages d'erreurs

    Script :
    Code POWERSHELL : 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
    #CREATION du repertoire
    # $DateJ = Get-Date -format yyyy-MM
    $DateJ ="2021-10"
     
    $path = "C:\TRAITEMENT\DB-EMPLOYEE\SOURCE\"+$DateJ
    If(!(test-path $path))
    {
          New-Item -ItemType Directory -Force -Path $path
    }
     
    $path2 = "C:\TRAITEMENT\DB-EMPLOYEE\DESTI\"+$DateJ
    If(!(test-path $path2))
    {
          New-Item -ItemType Directory -Force -Path $path2
    }
     
    $file = $path2 +"\Global_test.xls"
    $EMPLOYEE_GLOBAL =$path2+"\DB-EMPLOYEE-GLOBAL_"+$DateJ+".csv"
     
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $True
    $excel.DisplayAlerts = $False
     
    $workbook = $excel.Workbooks.Add()
     
    $workbook.Worksheets(1).Name = "TOTALITE"
    $workbook.Worksheets(2).Name = "ACTIF"
    $workbook.Worksheets(3).Name = "HORS"
     
    # Build the QueryTables.Add command and reformat the data
    $worksheet = $workbook.Worksheets(1)
    $TxtConnector = ("TEXT;" + $EMPLOYEE_GLOBAL)
    $Connector = $worksheet.QueryTables.add($TxtConnector, $worksheet.Range("A1"))
    $query = $worksheet.QueryTables.item($Connector.name)
    $query.TextFileOtherDelimiter = ";"
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1
     
    # Execute & delete the import query
    $query.Refresh()
    $query.Delete()
     
    $workbook.SaveAs([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) + $file )
    $workbook.Close()
    $excel.Quit()


    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
    Index non valide. (Exception de HRESULT : 0x8002000B (DISP_E_BADINDEX))
    Au caractère Ligne:27 : 1
    + $workbook.Worksheets(2).Name = "ACTIF"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    Index non valide. (Exception de HRESULT : 0x8002000B (DISP_E_BADINDEX))
    Au caractère Ligne:28 : 1
    + $workbook.Worksheets(3).Name = "HORS"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
     
    True
    Nous n’avons pas pu accéder au fichier. Essayez l’une des options suivantes :
    • Assurez-vous que le dossier spécifié existe. 
    • Assurez-vous que le dossier contenant le fichier n’est pas en mode Lecture seule.
    • Assurez-vous que le nom du fichier et le chemin du dossier ne contiennent pas l’un des caractères suivants :  <  >  ?  [  ]  :  | ou  *
    • Assurez-vous que le nom du fichier et le chemin du dossier ne contiennent pas plus de 218 caractères.
    Au caractère Ligne:44 : 1
    + $workbook.SaveAs([System.Environment]::GetFolderPath([System.Environm ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

  6. #6
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Je crois comprendre ce qui en va pas, les feuilles 2 et 3 semble ne pas exister dans ton "template".

    $EMPLOYEE_GLOBAL contient bien le chemin complet faire un fichier csv existant et valide ?
    Si oui essaie comme ceci
    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    $TxtConnector = ("TEXT;$EMPLOYEE_GLOBAL")

  7. #7
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Je vais être un peu occuper de suite mais je regarderai pour le nommages des feuilles.
    Tu peux me confirmer si visuellement la première est bien renommé et si il n'y en a qu'une qui existe ?

  8. #8
    Membre éclairé
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2014
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2014
    Messages : 326
    Par défaut
    le fichier excel est a 0 ko impossible d'ouvrir le ficiher car son format ou extention n'est pas valide.

    Effectivement les feuilles n'existent pas je veux creer le fichier xlsx pendant mon script. ceci me permettra par la suite d'adapter ce script pour d'autres actions.

  9. #9
    Membre éclairé
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2014
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2014
    Messages : 326
    Par défaut
    Bonsoir

    voici mon code modifié il créé bien les feuilles mais je bug sur la copie vers les feuilles


    Code POWERSHELL : 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
    #CREATION du repertoire
    # $DateJ = Get-Date -format yyyy-MM
    $DateJ ="2021-10"
     
    $path = "C:\TRAITEMENT\DB-EMPLOYEE\SOURCE\"+$DateJ
    If(!(test-path $path))
    {
          New-Item -ItemType Directory -Force -Path $path
    }
     
    $path2 = "C:\TRAITEMENT\DB-EMPLOYEE\DESTI\"+$DateJ
    If(!(test-path $path2))
    {
          New-Item -ItemType Directory -Force -Path $path2
    }
     
    $file = $path2 +"\Global_test.xlsx"
    $EMPLOYEE_GLOBAL =$path2+"\DB-EMPLOYEE-GLOBAL_"+$DateJ+".csv"
     
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $True
    $excel.DisplayAlerts = $False
     
    $workbook = $excel.Workbooks.Add()
    $workbook.worksheets.add()
    $workbook.worksheets.add()
    $workbook.worksheets("Feuil3").name = "GLOBAL"
    $workbook.worksheets("Feuil1").name = "HORS"
    $workbook.worksheets("Feuil2").name = "ACTIF"
     
    $workbook.SaveAs($file)
    $workbook.close($true) # close and save destination workbook
    $excel.Quit()
     
    $wb1 = $excel.workbooks.open($file) 
    $wb2 = $excel.workbooks.open($EMPLOYEE_GLOBAL)
    $ws2 = $wb2.WorkSheets(1) 
    $ws2.activate()  
    $range = $ws2.Range("A1").currentregion.Copy()
     
    $ws1 = $wb1.Worksheets("GLOBAL").Range("A1").Paste()
     
    $wb1.close($true)
    $wb2.close($false)
     
    $excel.Quit()


    Message d'erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Index non valide. (Exception de HRESULT : 0x8002000B (DISP_E_BADINDEX))
    Au caractère Ligne:43 : 1
    + $ws2 = $wb2.Worksheets("GLOBAL").Range("A1").Paste
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
    J'ai le process excel qui reste ouvert (dans le gestionnaire de tache) comment le fermer automatiquement ?

  10. #10
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Cool !
    Maintenant ceci devrait fonctionner.

    Attention à bien :
    - spécifier le délimiteur de ton fichier csv ici $query.TextFileOtherDelimiter = ";"
    - vérifier le chemin de $EMPLOYEE_GLOBAL
    - Vérifier que le fichier csv est "correcte"

    Code powershell : 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
    #CREATION du repertoire
    # $DateJ = Get-Date -format yyyy-MM
    $DateJ ="2021-10"
     
    $path = "C:\TRAITEMENT\DB-EMPLOYEE\SOURCE\"+$DateJ
    If(!(test-path $path))
    {
          New-Item -ItemType Directory -Force -Path $path
    }
     
    $path2 = "C:\TRAITEMENT\DB-EMPLOYEE\DESTI\"+$DateJ
    If(!(test-path $path2))
    {
          New-Item -ItemType Directory -Force -Path $path2
    }
     
    $file = $path2 +"\Global_test.xlsx"
    $EMPLOYEE_GLOBAL =$path2+"\DB-EMPLOYEE-GLOBAL_"+$DateJ+".csv"
     
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $True
    $excel.DisplayAlerts = $False
     
    $workbook = $excel.Workbooks.Add()
    $workbook.worksheets.add()
    $workbook.worksheets.add()
    $workbook.worksheets("Feuil3").name = "GLOBAL"
    $workbook.worksheets("Feuil1").name = "HORS"
    $workbook.worksheets("Feuil2").name = "ACTIF"
     
     
    # Build the QueryTables.Add command and reformat the data
    $worksheet = $workbook.Worksheets(1)
    $TxtConnector = ("TEXT;$EMPLOYEE_GLOBAL")
    $Connector = $worksheet.QueryTables.add($TxtConnector, $worksheet.Range("A1"))
    $query = $worksheet.QueryTables.item($Connector.name)
    $query.TextFileOtherDelimiter = ";"
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1
     
    # Execute & delete the import query
    $query.Refresh()
    $query.Delete()
     
     
    $workbook.SaveAs($file)
    $workbook.close($true) # close and save destination workbook
    $excel.Quit()

Discussions similaires

  1. [XL-2010] Importer plusieurs csv dans un seul classeur Excel
    Par matthieu44 dans le forum Macros et VBA Excel
    Réponses: 38
    Dernier message: 14/02/2017, 11h04
  2. Réponses: 17
    Dernier message: 24/02/2016, 11h03
  3. [C#2005]Excel2003 copier plusieurs classeurs dans un seul
    Par Herlece dans le forum Windows Forms
    Réponses: 2
    Dernier message: 25/08/2008, 21h12
  4. Copier plusieurs classeurs dans 1 seul
    Par iAlph dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 13/05/2008, 19h16
  5. Copier plusieurs TLabel dans le presse papier
    Par Manopower dans le forum Delphi
    Réponses: 3
    Dernier message: 26/09/2006, 10h30

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