Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.lang.visual-basic > #20010

Re: leggere file txt e popolare celle Excel

From "Sauro" <vicchi@crsscala.it>
Newsgroups it.comp.lang.visual-basic
Subject Re: leggere file txt e popolare celle Excel
Date 2022-12-15 17:00 +0100
Organization Aioe.org NNTP Server
Message-ID <tnfgb2$1h6t$1@gioia.aioe.org> (permalink)
References <d8b29811-7c2e-48ce-9f1a-a51b2b66ad19n@googlegroups.com>

Show all headers | View raw


Ciao Marco

Le funzioni che seguiranno non rispoondono esattamente
alle tue domande ma possono essere utili per raggiungere
i tuoi obbiettivi.

Leggi il testo di un file txt in una stringa (LeggiFileASCII)
Trovi quante righe a questa stringa (Memolines)

Col seguente ciclo leggi tutte le righe ad una ad una.
For r = 1 to Tutte_le_righe
    riga = MemoLine ( StringaTesto , r )
Next r

Ciao


Function LeggiFileASCII(PathOrigine As String) As String
' Ritorna il testo di un file ASCII (txt)
Dim sTextLine As String, lRestoFile As Long
Dim nFileOrigine As Integer
Dim lDimensioneFile As Long, lCounter As Long
Dim Testo As String
   On Error GoTo LeggiFileASCII_Error

nFileOrigine = FreeFile
Open PathOrigine For Binary As #nFileOrigine ' Apre il file.
' imposto variabili base
lCounter = 1
lDimensioneFile = LOF(nFileOrigine)
sTextLine = String(4000, ".") '
' restituisce la dimensione restante dopo tutti i
' blocchi di byte
lRestoFile = lDimensioneFile - Int(lDimensioneFile / 4000) * 4000
Do While Not EOF(nFileOrigine) ' Ripete fino alla fine del file.
     sTextLine = String(4000, ".") '
     If lCounter < Int(lDimensioneFile / 4000) * 4000 Then
         ' leggo e copio blocco dati
         ' BLOCCO INTERO
         Get #nFileOrigine, lCounter, sTextLine
         lCounter = lCounter + 4000
         Testo = Testo & sTextLine
     Else
        ' reimposto txtline sui bytes restanti
        ' BLOCCO ULTIMO PARZIALE
         sTextLine = String(lRestoFile, ".")
         Get #nFileOrigine, lCounter, sTextLine
         Testo = Testo & sTextLine
         Exit Do
     End If
Loop
If InStr(Testo, Chr(26)) > 0 Then 'Aggiunto per files creati con clipper
    Testo = Left(Testo, InStr(Testo, Chr(26)) - 1)
End If
Close #nFileOrigine    ' Chiude il file.
LeggiFileASCII = Testo

   On Error GoTo 0
   Exit Function

LeggiFileASCII_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure 
LeggiFileASCII of Modulo Proc_Memo"
End Function




Public Sub ScriviFileASCII(PathDestinazione As String, TestoFile As String)
' Scrive un testo in un file ASCII (txt)
    If Dir(PathDestinazione) <> "" Then Kill PathDestinazione
    Dim nFileDestinazione As Integer
    nFileDestinazione = FreeFile
    ' APRO  FILE DI DESTINAZIONE
    Open PathDestinazione For Binary Access Write As #nFileDestinazione ' 
Apre il file.
    Put #nFileDestinazione, , TestoFile
    Close #nFileDestinazione
End Sub




Public Function MemoLines(ilTesto As String) As Single
' Ritorna il numero di righe in una stringa
Dim VarVariant As Variant
VarVariant = Split(ilTesto, vbCrLf)
MemoLines = (UBound(VarVariant) - LBound(VarVariant)) + 1
End Function



Public Function MemoLine(ilTesto As String, QualeRiga As Variant) As String
' Ritorna la stringa corrispondente alla riga voluta
If Len(ilTesto) < 1 Then
    MemoLine = ""
    Exit Function
End If
Dim VarVariant As Variant
VarVariant = Split(ilTesto, vbcrflf)
MemoLine = VarVariant(QualeRiga - 1)
End Function




Back to it.comp.lang.visual-basic | Previous | NextPrevious in thread | Find similar


Thread

leggere file txt e popolare celle Excel Marco75 <marcoporzi75@gmail.com> - 2022-12-02 06:43 -0800
  Re: leggere file txt e popolare celle Excel Marco75 <marcoporzi75@gmail.com> - 2022-12-02 07:03 -0800
  Re: leggere file txt e popolare celle Excel "Sauro" <vicchi@crsscala.it> - 2022-12-15 17:00 +0100

csiph-web