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:
How can I do this with Visual Basic?
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:
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: