Remarque : Dans UDF_ExcelCOM il existe aussi une fonction nommé _ExcelBookOpenTxt(...) permettant d'ouvrir un fichier texte avec Excel, mais cette fonction ne prend pas en compte toutes les options (notamment le FieldInfo).
- csv_to_excel.zip
- (17.54 Kio) Téléchargé 438 fois
► Afficher le texteMontrer le code
Code : Tout sélectionner
#cs -----------------------------------------------------------------------------------------------------
AutoIt Version : 3.2.12.1
Author : 20100
Requirement : UDF_ExcelCOM 1.4
Exemple de conversion d'un fichier CSV en un fichier Excel
Le délimiteur utilisé dans le fichier CSV sera le caractère "|"
Voici le contenu du fichier CSV :
prenom|age|reference|date
pierre|18|028DFGXX|18/08/1985
paul|35|582ERMLJ|23/01/1975
jack|22|689MJFIE|06/11/2001
Nous allons utilisé la méthode Workbooks.OpenText pour ouvrir le fichier CSV avec Excel et voir les
options qui entrent en jeu dans cette méthode. Il exite bien une méthode dans UDF_ExcelCOM nommé
_ExcelBookOpenTxt(...) mais il ne prend pas en compte toutes les options, notamment le FieldInfo
#ce -----------------------------------------------------------------------------------------------------
#include <UDF_ExcelCOM.au3>
Opt('MustDeclareVars', 1) ; Obligation de déclarer toutes variables avant d'être utilisées.
Local $chemin_fichier_csv_source = @WorkingDir&"\fichier_source_test_csv.txt" ; Chemin relatif du fichier csv de test
Local $oExcel = ObjCreate("Excel.Application") ; Création d'une nouvelle instance Excel
If IsObj($oExcel) Then
$oExcel.Visible = 0 ; Pour rendre Excel invisible.
; Déclaration du type de colonne pour le paramètre FieldInfo
local $datatype_colonne1[2]=[0,2] ; Colonne 1 "prenom" en format Texte
local $datatype_colonne2[2]=[1,2] ; Colonne 2 "age" en format Texte
local $datatype_colonne3[2]=[2,2] ; Colonne 3 "reference" en format Texte
local $datatype_colonne4[2]=[3,4] ; Colonne 4 "date" en format jj/mm/aaaa
local $datatype_array[4] = [$datatype_colonne1,$datatype_colonne2,$datatype_colonne3,$datatype_colonne4]
; Paramètres de Workbooks.OpenText
;===========================================================================================================
; Workbooks.OpenText($Filename,$Origin,$StartRow,$DataType,$TextQualifier,$Consecutive,$Tab,$Semicolon,
; $Comma,$Space,$Other,$OtherChar,$FieldInfo)
;
; 1) Filename (Required) String Specifies the file name of the text file to be opened and parsed.
; 2) Origin (Optional) Specifies the origin of the text file. (xlWindows=2, xlMSDOS or Default)
; 3) StartRow (Optional) The row number at which to start parsing text.
; 4) DataType (Optional) Précise si les colonnes sont delimitées ou de taille fixe (1=delimited, 2=fixedwidth)
; 5) TextQualifier (Optional) Specifies the text qualifier. (1=double quote, -4142=delimited, 2=single quote)
; 6) Consecutive (Optional) True to have consecutive delimiters considered one delimiter.
; 7) Tab (Optional) True to have the tab character be the delimiter (DataType must be xlDelimited=1)
; 8) Semicolon (Optional) True to have the semicolon character be the delimiter (DataType must be xlDelimited=1)
; 9) Comma (Optional) True to have the comma character be the delimiter (DataType must be xlDelimited=1)
; 10) Space (Optional) True to have the space character be the delimiter (DataType must be xlDelimited=1)
; 11) Other (Optional) True to have an anoter character delimiter specified by the OtherChar argument (DataType must be xlDelimited=1)
; 12) OtherChar (Optional) Required if Other is True, specifies the delimiter character when Other is True.
; If more than one character is specified, only the first character of the string is used,
; the remaining characters are ignored.
; 13) FieldInfo (Optional) XlColumnDataType. An array containing parse information for individual columns of data.
; Il s'agit d'une suite d'Array(IndexColonne, TypeColonne) où
; - IndexColonne correspond à l'index de la première colonne
; - TypeColonne au type de la colonne, dont voici la liste possible :
; o xlGeneralFormat Général : 1
; o xlTextFormat Texte : 2
; o xlMDYFormat Format de date Mois-Jour-Année : 3
; o xlDMYFormat Format de date Jour-Mois-Année : 4
; o xlYMDFormat Format de date Année-Mois-Jour : 5
; o xlMYDFormat Format de date Mois-Année-Jour : 6
; o xlDYMFormat Format de date Jour-Année-Mois : 7
; o xlYDMFormat Format de date Année-Jour-Mois : 8
; o xlEMDFormat Date EMD : 9
; o xlSkipColumn Non distribuée : 10
;===========================================================================================================
$oExcel.Workbooks.OpenText($chemin_fichier_csv_source,2,1,1,1,False,False,False,False,False,True,"|",$datatype_array)
; Renommer l'onglet actif en DONNEES
_ExcelSheetNameSet($oExcel, "DONNEES")
; Sauvegarde de l'instance $oExcel dans un nouveau fichier "fichier_converti_excel.xls" dans le répertoire courant du script
Local $chemin_fichier_xls_destination = @WorkingDir&"\fichier_converti_excel"
_ExcelBookSaveAs($oExcel, $chemin_fichier_xls_destination, "xls", 0, 1)
_ExcelBookClose($oExcel, 0, 0) ; Fermeture de l'instance d'Excel sans sauvegarde et sans message d'alerte
EndIf