Nu har jeg leget lidt med ovennævnte link og rettet til og fået det til at fungere.
Mvh
sMorch
default2.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblsplittext" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
default2.aspx.vb:
Imports System
Imports System.IO
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'OpenMyText()
ReadFromFile("c:\stien\til\teksfilen\test.txt") 'RET stien og filnavnet
End Sub
Public Sub ReadFromFile(ByVal strPath As String)
Dim FileHolder As FileInfo = New FileInfo(strPath)
Dim ReadFile As StreamReader = FileHolder.OpenText()
Dim strLine As String
Do
strLine = ReadFile.ReadLine
If Not strLine = "" Then
ParseCSV(strLine)
'MsgBox("ENDLINE")
lblsplittext.Text &= "<br>"
Else
ReadFile.Close()
ReadFile = Nothing
'lblsplittext.Text &= "hmmmmm"
End If
Loop Until ReadFile Is Nothing
'Return True
End Sub
Public Sub ParseCSV(ByVal CSVstr As String)
Dim startPos As Integer
Dim endPos As Integer
Dim currPos As Integer
Dim tempstr As String
Dim commaPos As Integer
Dim quotePos As Integer
Dim strLen As Integer
Dim charLen As Integer
startPos = 1
currPos = 1
strLen = Len(CSVstr)
Do While strLen <> 0
CSVstr = Replace(CSVstr, Chr(34) & Chr(34), "'")
commaPos = InStr(currPos, CSVstr, ",")
quotePos = InStr(currPos, CSVstr, Chr(34))
'last data
If commaPos = 0 Then
If quotePos = 0 Then
If Not currPos > endPos Then
endPos = strLen + 1
charLen = endPos - currPos
tempstr = Mid(CSVstr, currPos, charLen)
'If Not tempstr = "" Then
lblsplittext.Text &= ReadChars(tempstr, 1, charLen, charLen) & " " 'her er sat et mellemrum i enden for sys skyld - ditto i nedenstående ens linier
'End If
End If
Else
currPos = quotePos
endPos = InStr(currPos + 1, CSVstr, Chr(34))
charLen = endPos - currPos
tempstr = Mid(CSVstr, currPos + 1, charLen - 1)
'If Not tempstr = "" Then
lblsplittext.Text &= ReadChars(tempstr, 1, charLen, charLen) & " "
'End If
End If
Exit Do
End If
'no " in line
If quotePos = 0 Then
endPos = commaPos
charLen = endPos - currPos
tempstr = Mid(CSVstr, currPos, charLen)
'If Not tempstr = "" Then
lblsplittext.Text &= ReadChars(tempstr, 1, charLen, charLen) & " "
'End If
ElseIf (quotePos <> 0) Then
'" in line
If commaPos < quotePos Then
endPos = commaPos
charLen = endPos - currPos
tempstr = Mid(CSVstr, currPos, charLen)
'If Not tempstr = "" Then
lblsplittext.Text &= ReadChars(tempstr, 1, charLen, charLen) & " "
'End If
Else
currPos = quotePos
endPos = InStr(currPos + 1, CSVstr, Chr(34))
charLen = endPos - currPos
tempstr = Mid(CSVstr, currPos + 1, charLen - 1)
'If Not tempstr = "" Then
lblsplittext.Text &= ReadChars(tempstr, 1, charLen, charLen) & " "
'End If
endPos = endPos + 1
End If
End If
currPos = endPos + 1
Loop
End Sub
Public Function ReadChars(ByVal str As String, ByVal StartPos As Integer, ByVal EndPos As Integer, ByVal strLen As Integer) As String
Dim strArray As [String] = str
Dim b(strLen) As Char
Dim sr As New StringReader(strArray)
sr.Read(b, 0, EndPos)
Return b
'lblsplittext.Text &= b ' & " "
'MsgBox(b)
sr.Close()
End Function
End Class
test.txt:
"ITEM00115","Cykelhjelm, Large","L","DKK","499,95","",""
"ITEM00116","Cykelhjelm, Medium","M","DKK","359,95","",""
"ITEM00117","Cykelhjelm, Small","S","DKK","229,95","",""