Question csv-File to html-File ???

Gooner85

New member
Joined
Jan 21, 2009
Messages
2
Programming Experience
Beginner
I want to write the data from a csv-file into a html-file.

The CSV contains a list of information like:
post code; street; house number;
I use ";" as delimiter!

Now I want to put this information in a html-table.
So I want to have one gap with all post codes, one with all streets.

The headline of the html-table already exists!

I'm using Visual Basic .NET with Visual Studio 2005!

Here is the code for Power Basic: :confused:
VB.NET:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   file       csv2html.bas
'   funktion   csv daten in html Tabelle einlesen und html file erstellen
'   inputs     html template    ( recruter.htm )
'              csv Data file    ( maerkte.csv )
'   outputs    html mit Tabelle ( personal.htm )
'              error/log file   ( csv2html.log )

#DIM ALL
#COMPILE EXE
#REGISTER NONE
#OPTION VERSION4
#INCLUDE "win32api.inc"


'' Konstanten
$fnTemplate = "recruter.htm"
$fnCsvData  = "maerkte.csv"
$fnHTMLout  = "personal.htm"
$fnError    = "csv2html.log"

$TagStart   = "<!start line>"
$TagEnd     = "<!end line>"



FUNCTION FileExists(BYVAL filename AS STRING) AS LONG
    ON ERROR RESUME NEXT
    FUNCTION = (DIR$(filename) > "")
END FUNCTION



FUNCTION logfile ( BYVAL sMsg AS STRING ) AS LONG

    LOCAL hFile AS LONG
    ON ERROR GOTO logfile_err

    hFile = FREEFILE

    OPEN $fnError FOR APPEND AS hfile
    PRINT #hfile, sMsg & $CRLF
    CLOSE #hfile

    FUNCTION = %true
    EXIT FUNCTION

logfile_err:
    CLOSE hfile
END FUNCTION


FUNCTION readTemplate() AS STRING

    LOCAL hFile AS LONG
    LOCAL sTemp AS STRING

    IF ISFALSE FileExists( $fnTemplate ) THEN
       logfile( "Folgende Datei fehlt: " & $fnTemplate )
       EXIT FUNCTION
    END IF

    hFile = FREEFILE
    OPEN $fnTemplate FOR BINARY AS #hFile
    GET$ #hFile , LOF(#hFile), sTemp       'copy file into string
    CLOSE #hFile

'''' steuer tags vorhanden ?
    IF INSTR( sTemp, $TagStart ) = 0 THEN
       logfile( "Der Steuer Tag " & $TagStart & " fehlt in der Datei : " & $fnTemplate )
       EXIT FUNCTION
    END IF
    IF INSTR( sTemp, $TagEnd ) = 0 THEN
       logfile( "Der Steuer Tag " & $TagEnd & " fehlt in der Datei : " & $fnTemplate )
       EXIT FUNCTION
    END IF

    FUNCTION = sTemp
END FUNCTION



FUNCTION readCSV() AS STRING

    LOCAL hFile AS LONG
    LOCAL sTemp AS STRING

    IF ISFALSE FileExists( $fnCsvData ) THEN
       logfile( "Folgende Datei fehlt: " & $fnCsvData )
       EXIT FUNCTION
    END IF

    hFile = FREEFILE
    OPEN $fnCsvData FOR BINARY AS #hFile
    GET$ #hFile , LOF(#hFile), sTemp       'copy file into string
    CLOSE #hFile

'''' feld definitionen vorhanden ?
    IF PARSECOUNT(sTemp, ";") = 0 THEN
       logfile( "Die Datei : " & $fnCsvData  & " hat das falsche Format." )
       EXIT FUNCTION
    END IF

    FUNCTION = sTemp
END FUNCTION



SUB splitTemplate( sTemp AS STRING, sKopf AS STRING, sFuss AS STRING, sFlds AS STRING )
    LOCAL iStart  AS INTEGER
    LOCAL iLength AS INTEGER

    sKopf   = EXTRACT$(sTemp, $TagStart)
    sFuss   = REMAIN$(sTemp, $TagEnd)
    iStart  = LEN(sKopf)+ LEN($TagStart)+1
    iLength = LEN(sTemp)-iStart-LEN(sFuss)-LEN($TagEnd)
    sFlds   = LCASE$( MID$(sTemp,iStart,iLength) )         ''' für feldnamen array

END SUB

FUNCTION WINMAIN ( BYVAL hInstance   AS DWORD,      BYVAL hPrevInst AS DWORD, _
                   BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow  AS LONG ) AS LONG

    LOCAL i        AS INTEGER
    LOCAL lines    AS INTEGER
    LOCAL curLine  AS INTEGER
    LOCAL fldCount AS INTEGER

    LOCAL hFile AS LONG

    LOCAL sTemp AS STRING
    LOCAL sKopf AS STRING
    LOCAL sFuss AS STRING
    LOCAL sFlds AS STRING
    LOCAL sNew  AS STRING
    LOCAL sOut  AS STRING
    LOCAL sLine AS STRING


    sTemp = readTemplate()                        ''' Quelldaten lesen & validieren
    IF LEN(sTemp) = 0 THEN EXIT FUNCTION
    splitTemplate sTemp, sKopf, sFuss, sFlds

    sTemp = readCSV()
    IF LEN(sTemp) = 0 THEN EXIT FUNCTION

    lines = PARSECOUNT(sTemp, $CRLF)              ''' zeilen der csv Datei zählen
    sLine = PARSE$(sTemp, $CRLF,1)                ''' Kopfzeile auslesen

    fldCount = PARSECOUNT(sLine, ";")             ''' Felder der Kopfzeile zählen
    DIM flsLst(fldCount) AS LOCAL STRING          ''' array mit feldnamen füllen

    FOR i = 1 TO fldCount
        flsLst(i) = LCASE$(PARSE$(sLine, ";", i)) ''' lcase wegen lcase(sFlds)
    NEXT i

    FOR curLine = 2 TO lines
        sLine = PARSE$(sTemp, $CRLF,curLine)      ''' datenzeile auslesen
        sNew  = sFlds                             ''' Arbeitsstring mit HTML FeldIDs
        FOR i = 1 TO fldCount
            REPLACE  "--"+flsLst(i)+"--" WITH PARSE$(sLine, ";", i) IN sNew
        NEXT i
        sOut = sOut + sNew
    NEXT

    hFile = FREEFILE                              ''' zieldaten speichern
    OPEN $fnHTMLout FOR OUTPUT AS #hFile
    PRINT #hFile , sKopf + sOut + sFuss
    CLOSE #hFile
    logfile( "Datei " & $fnHTMLout & " neu am: " & _
             TIME$ & DATE$ & " Records: " + STR$(lines) )

END FUNCTION

How can I do this with Visual Basic?
 
Last edited:
Hello.

You know, your chances of getting an answer would be a lot greater if you would tell us what the HTML should look like, instead of showing a big block of code which needs to be ported without further informations.

Should the HTML for example be a table?

Bobby
 
One way is to add the fundamental snippet "read delimited text file", then use a StreamWriter to write the text lines for Html document and put all the fields into table cells. I will post the snippet template here so you see how little code I've actually added later, this is given by VS code editor:
VB.NET:
Dim filename As String = "C:\Test.txt"
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
    parser.SetDelimiters(delimiter)
    While Not parser.EndOfData
        ' Read in the fields for the current line
        fields = parser.ReadFields()
        ' Add code here to use data in fields variable.

    End While
End Using
Then adding code to write text file could be something like this:
VB.NET:
Dim writer As New IO.StreamWriter("output.html")
writer.WriteLine("<html><head>")
writer.WriteLine("<style type=""text/css"">td {border:solid 1px blue; width:50px}</style>")
writer.WriteLine("</head><body><table>")

Dim filename As String = "data.csv"
Dim fields As String()
Dim delimiter As String = ";"
Using parser As New TextFieldParser(filename)
    parser.SetDelimiters(delimiter)
    While Not parser.EndOfData
        ' Read in the fields for the current line
        fields = parser.ReadFields()
        ' Add code here to use data in fields variable.
        writer.WriteLine("<tr>")
        For Each field As String In fields
            writer.WriteLine("   <td>{0}</td>", field)
        Next
        writer.WriteLine("</tr>")
    End While
End Using
writer.WriteLine("</table></body></html>")
writer.Close()
Customize as you see fit, for example if csv file has headers you can add some code that takes first line of fields and put into a row of TH cells.
 
Back
Top