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

Macros et VBA Excel Discussion :

CSV sauvegardé en UTF8


Sujet :

Macros et VBA Excel

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut CSV sauvegardé en UTF8
    Bonjour,

    Je fais sauvegarde de fichier .csv que je crée moi même via une macro excel. Tout ceci fonctionne correctement.
    Mon probleme est que ej voudrais que le ficheir csv soit sauvegardé en UTF8.
    Est-ce possible? Si oui, comment faire car je n'ais rien trouvé.

    Merci

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    Tu peux regarder du côté de l'objet DefaultWebOptions. D'après ce que j'en retire, l'encodage n'a d'intérêt que pour les pages Web.

    Tu pourraris tenter une sauvegarde sous forme de page Web UTF-8 et ensuite convertir ton fichier en .csv...

  3. #3
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Par curiosité, dans quelle version es-tu ? Je n'ai rien trouvé sur UTF dans 2003.

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    Re à tous,

    Pas mal debordé au taff. Je cherche à savoir ceci car le csv va passer par sap apres. C'est pas moi qui gere ça et on m'a dit que mon fichier était pas en UTF-8.
    Sinon version office, quelquonque, faut que ça passe partout.

  5. #5
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    Citation Envoyé par Singular Voir le message
    Tu peux regarder du côté de l'objet DefaultWebOptions. D'après ce que j'en retire, l'encodage n'a d'intérêt que pour les pages Web.

    Tu pourraris tenter une sauvegarde sous forme de page Web UTF-8 et ensuite convertir ton fichier en .csv...
    T'as essayé?

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    Bon j'ai essayé vos propositions, mais j'ai pas du tout pigé, car j"y suis pas arrivé. Peut être du au fait que j'enregistre ainsi:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    ...
     
        On Error Resume Next
        Kill file
        Err.Clear
        Open file For Binary As #1
        Put #1, , Buff
        Close #1
    ...
    Désolé de répondre en décousu.
    Sinon, aucune manipulation utilisateur possible. Je ne l'avais pas préciser.

  7. #7
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    'Open ... For Binary' ne crée pas un fichier texte mais binaire (langage machine). Ainsi, le résultat ne sera pas un Comma Seperated Values (Valeurs Séparées par Virgules), mais plutôt un genre de fichier qui contient des (%6469&%8375&ehdu64HRUIGO...) et plein de caractères non typographiques.

    Serts-toi plutôt de 'Open ... For Output' pour créer/écrire dans un fichier texte ou 'Open ... For Append' pour ajouter à un fichier existant. Pour écrire une ligne de texte, serts-toi de 'Print'. Tu verra un bon résumé à cette adresse http://warin.developpez.com/access/fichiers/.

    N'oublies pas qu'il faut concaténer tes Strings avant d'utiliser Print pour les écrire. Ex.:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        Dim sLigne As String
        Dim f as Integer
     
        f = FreeFile()
        Open "C:\monfichier.csv" For Output As #f
        sLigne = "Hello" & "," & "World" & "," & "!"
        Print #f, sLigne
        Close #f

  8. #8
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    Merci pour l'explication, mais mon fichier est bien généré en CSV. Mon seul probleme est l'encodage qui est en ANSI et non en UTF8.

    Je vais aller voir ton lien voir s'il y a quelque chose sur l'utf8

  9. #9
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    Tu n'as pas essayé de l'enregistrer sous forme html-utf8 et ensuite d'enregistrer le html-utf8 en csv?

    Dis-moi...

  10. #10
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    re,

    ça se fait grace au DefaultWebOptions l'enregistrement en html-utf8?

    Ou avec un truc du genre saveAS?

    J'avouerais, que je suis un peu paumé a force là. Testé un peu trop de choses qui ne me donne pas le résultat escompté.

    Sinon, Singular, j'ai regardé ton lien, j'y ai rappri des choses, mais toujours pas mon probléme de régler. Merci de ton aide.

  11. #11
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    J'ai trouvé ce code sur un autre forum:

    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    Code de test :
     
    Sub main()
        Debug.Print Encode_UTF8("œ" )
        Debug.Print Decode_UTF8(Encode_UTF8("œ" ))
        Debug.Print Decode_UTF8("éa" )
        Debug.Print isUTF8("éa" )
        Debug.Print isUTF8("abcde" )
    End Sub
     
     
    Code principal :
     
    Option Explicit
     
    '   Char. number range  |        UTF-8 octet sequence
    '      (hexadecimal)    |              (binary)
    '   --------------------+---------------------------------------------
    '   0000 0000-0000 007F | 0xxxxxxx
    '   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
    '   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
    '   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    Public Function Encode_UTF8(astr)
        Dim c
        Dim n
        Dim utftext
     
        utftext = ""
        n = 1
        Do While n <= Len(astr)
            c = AscW(Mid(astr, n, 1))
            If c < 128 Then
                utftext = utftext + Chr(c)
            ElseIf ((c >= 128) And (c < 2048)) Then
                utftext = utftext + Chr(((c \ 64) Or 192))
                utftext = utftext + Chr(((c And 63) Or 128))
            ElseIf ((c >= 2048) And (c < 65536)) Then
                utftext = utftext + Chr(((c \ 4096) Or 224))
                utftext = utftext + Chr((((c \ 64) And 63) Or 128))
                utftext = utftext + Chr(((c And 63) Or 128))
            Else ' c >= 65536
                utftext = utftext + Chr(((c \ 262144) Or 240))
                utftext = utftext + Chr(((((c \ 4096) And 63)) Or 128))
                utftext = utftext + Chr((((c \ 64) And 63) Or 128))
                utftext = utftext + Chr(((c And 63) Or 128))
            End If
            n = n + 1
        Loop
        Encode_UTF8 = utftext
    End Function
     
    '   Char. number range  |        UTF-8 octet sequence
    '      (hexadecimal)    |              (binary)
    '   --------------------+---------------------------------------------
    '   0000 0000-0000 007F | 0xxxxxxx
    '   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
    '   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
    '   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    Public Function Decode_UTF8(astr)
        Dim c0, c1, c2, c3
        Dim n
        Dim unitext
     
        If isUTF8(astr) = False Then
            Decode_UTF8 = astr
            Exit Function
        End If
     
        unitext = ""
        n = 1
        Do While n <= Len(astr)
            c0 = Asc(Mid(astr, n, 1))
            If n <= Len(astr) - 1 Then
                c1 = Asc(Mid(astr, n + 1, 1))
            Else
                c1 = 0
            End If
            If n <= Len(astr) - 2 Then
                c2 = Asc(Mid(astr, n + 2, 1))
            Else
                c2 = 0
            End If
            If n <= Len(astr) - 3 Then
                c3 = Asc(Mid(astr, n + 3, 1))
            Else
                c3 = 0
            End If
     
            If (c0 And 240) = 240 And (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 240) * 65536 + (c1 - 128) * 4096) + (c2 - 128) * 64 + (c3 - 128)
                n = n + 4
            ElseIf (c0 And 224) = 224 And (c1 And 128) = 128 And (c2 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 224) * 4096 + (c1 - 128) * 64 + (c2 - 128))
                n = n + 3
            ElseIf (c0 And 192) = 192 And (c1 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 192) * 64 + (c1 - 128))
                n = n + 2
            ElseIf (c0 And 128) = 128 Then
                unitext = unitext + ChrW(c0 And 127)
                n = n + 1
            Else ' c0 < 128
                unitext = unitext + ChrW(c0)
                n = n + 1
            End If
        Loop
     
        Decode_UTF8 = unitext
    End Function
     
    '   Char. number range  |        UTF-8 octet sequence
    '      (hexadecimal)    |              (binary)
    '   --------------------+---------------------------------------------
    '   0000 0000-0000 007F | 0xxxxxxx
    '   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
    '   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
    '   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    Public Function isUTF8(astr)
        Dim c0, c1, c2, c3
        Dim n
     
        isUTF8 = True
        n = 1
        Do While n <= Len(astr)
            c0 = Asc(Mid(astr, n, 1))
            If n <= Len(astr) - 1 Then
                c1 = Asc(Mid(astr, n + 1, 1))
            Else
                c1 = 0
            End If
            If n <= Len(astr) - 2 Then
                c2 = Asc(Mid(astr, n + 2, 1))
            Else
                c2 = 0
            End If
            If n <= Len(astr) - 3 Then
                c3 = Asc(Mid(astr, n + 3, 1))
            Else
                c3 = 0
            End If
     
            If (c0 And 240) = 240 Then
                If (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
                    n = n + 4
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 224) = 224 Then
                If (c1 And 128) = 128 And (c2 And 128) = 128 Then
                    n = n + 3
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 192) = 192 Then
                If (c1 And 128) = 128 Then
                    n = n + 2
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 128) = 0 Then
                n = n + 1
            Else
                isUTF8 = False
                Exit Function
            End If
        Loop
    End Function
    Je trouve la source intéressante, et je pense qu'elle va régler mon problème. Il ne me reste plus qu'à la validé.

    Mais je veux bien savoure quand même l'histoire du html-utf8 ...

    Merci à tous.

  12. #12
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    Tu peux toujours essayer ça et voir si le résultat passera le test.

    Au préalable, tu sauvegardes ton fichier en cvs (qui condiendra des ; par défaut).

    Ensuite, tu traites ce fichier par la macro, en précisant les chemins et noms de fichiers dans les variables appropriées au début de la macro.

    Donnes des nouvelles svp...

    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
    Sub TransformerEnUnicode()
        Dim i As Long
        Dim sNomFichierIn As String
        Dim sNomFichierOut As String
        Dim fIn As Integer
        Dim fOut As Integer
        Dim sLigne As String
     
        ' Préciser les chemins et noms des fichiers è traiter
        sNomFichierIn = ""
        sNomFichierOut = ""
     
        fIn = FreeFile()
        Open sNomFichierIn For Input As #fIn
     
        fOut = FreeFile()
        Open sNomFichierOut For Binary As #fOut
     
        Do While Not EOF(fIn)
            Line Input #fIn, sLigne
            For i = 1 To Len(sLigne)
                If Mid(sLigne, i, 1) = ";" Then
                    Put #fOut, , AscW(",")
                Else
                    Put #fOut, , AscW(Mid(sLigne, i, 1))
                End If
            Next i
            Put #fOut, , AscW(vbCrLf)
        Loop
        Close #fIn
        Close #fOut
    End Sub

  13. #13
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 163
    Par défaut
    Merci de ton aide, je test tout ça et je te donne une réponse.

Discussions similaires

  1. probleme csv et encodage utf8
    Par gp3434 dans le forum Général Python
    Réponses: 5
    Dernier message: 24/01/2012, 22h40
  2. sauvegarde d'un fichier excel csv sous le format xls
    Par blondelle dans le forum C++Builder
    Réponses: 5
    Dernier message: 01/08/2006, 11h30
  3. Sauvegarder les données dans un fichier CSV
    Par beb30 dans le forum MFC
    Réponses: 5
    Dernier message: 08/03/2006, 13h06
  4. Sauvegarde de données dans fichier csv
    Par issou dans le forum C
    Réponses: 1
    Dernier message: 22/12/2005, 08h36
  5. Réponses: 2
    Dernier message: 14/05/2004, 12h55

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