Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2012
    Messaggi
    30

    VB.NET Scrittura file csv

    Ciao a tutti,
    sto scrivendo un programma che mi legge un file csv e ne crea un secondo con tutte le informazioni del primo ma con qualche modifica.

    codice:
    Dim NomeFile As String = "C:\file1.csv"
    Dim NomeFileNew As String = "C:\file2.csv"
    
    FileOpen(FileNumber, NomeFile, OpenMode.Input)
    FileOpen(FileNumber2, NomeFileNew, OpenMode.Output)
    
    
    Do Until (EOF(FileNumber))
                ' Read a line from file 
                Dim Text As String = LineInput(FileNumber)
                ' Split line at commas 
                Dim Values() As String = Split(Text, ";")
    
    
    If i = 0 Then
                    WriteLine(FileNumber2, Text)
                Else
                    If Values(3) <> "" Then
                        Select Case Values(3).Substring(3, 3)
                            Case "GEN"
                                Values(3) = Values(3).Substring(0, 3) & "JAN" & Values(3).Substring(6, 3)
                            Case "FEB"
                                Values(3) = Values(3).Substring(0, 3) & "FEB" & Values(3).Substring(6, 3)
                            Case "MAR"
                                Values(3) = Values(3).Substring(0, 3) & "MAR" & Values(3).Substring(6, 3)
                            Case "APR"
                                Values(3) = Values(3).Substring(0, 3) & "APR" & Values(3).Substring(6, 3)
                            Case "MAG"
                                Values(3) = Values(3).Substring(0, 3) & "MAY" & Values(3).Substring(6, 3)
                            Case "GIU"
                                Values(3) = Values(3).Substring(0, 3) & "JUN" & Values(3).Substring(6, 3)
                            Case "LUG"
                                Values(3) = Values(3).Substring(0, 3) & "JUL" & Values(3).Substring(6, 3)
                            Case "AGO"
                                Values(3) = Values(3).Substring(0, 3) & "AUG" & Values(3).Substring(6, 3)
                            Case "SET"
                                Values(3) = Values(3).Substring(0, 3) & "SEP" & Values(3).Substring(6, 3)
                            Case "OTT"
                                Values(3) = Values(3).Substring(0, 3) & "OCT" & Values(3).Substring(6, 3)
                            Case "NOV"
                                Values(3) = Values(3).Substring(0, 3) & "NOV" & Values(3).Substring(6, 3)
                            Case "DIC"
                                Values(3) = Values(3).Substring(0, 3) & "DEC" & Values(3).Substring(6, 3)
                        End Select
                    End If
    
    j = 0
                    For Each str In Values
                        If j = 0 Then
                            Text = str
                        Else
                            Text = Text & ";" & str
                        End If
                        j = j + 1
                    Next
                    WriteLine(FileNumber2, Text)
                End If
                i = i + 1
            Loop
            FileClose(FileNumber)
            FileClose(FileNumber2)
    Il mio problema è che la stringa finale che viene scritta dalla writeline contine i doppi apici all'inizio e alla fine della stessa e questo non mi consente di effettuare l'importazione del file in un DB Access.
    Ecco un piccolo esempio di come si presenta il file csv:

    CD_PRAT;CE_ZONA;DE_ZONE;DA_CREA;CE_RSOC;
    123456879;ZONA1;NORD-OVEST (MI+IVR);05-GEN-12;Azienda xxx

    il nuovo file si presenta in questa maniera
    "CD_PRAT;CE_ZONA;DE_ZONE;DA_CREA;CE_RSOC"
    "123456879;ZONA1;NORD-OVEST (MI+IVR);05-JAN-12;Azienda xxx

    Sto impazzendo!!!!!!!
    Ho provato a copiare anche dalla posizione 1 alla len-1 della stringa ma mi tronca il primo carattere, come è possibile che vengano copiati i doppi apici anche se non sono presenti nella stringa?

    Grazie in anticipo
    Andrea

  2. #2
    Ciao, stai usando funzioni che fanno parte (per compatibilità) del vecchio vb6.
    Dovresti usare le funzioni del .NET.
    Comunque, per il tuo problema leggi qui: Writeline
    Jupynet

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2012
    Messaggi
    30
    Grazie Jupynet,
    risolto grazie al link che mi hai mandato!!!

    Se si utilizza la funzione println non mi aggiunge i fastidiosi doppi apici!!!

    Alla prossima!!!

    Andrea

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.