Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and Displaying a CSV file using ASP 1

Status
Not open for further replies.

agp4

Programmer
Oct 7, 2005
10
0
0
GB
Hi, can somebody point me in the right direction - I have a csv files of the following format;

"HDR","1","1","STORE NAME"
"LNE","1","M6-T"
"LNE","2","L6-T"
"HDR","2","1","STORE NAME"
"LNE","1","M6-T"
"EOF"

The HDR field represent a header, whilst the LNE fields represents lines/records. EOF represent the end of the file. Can somebody please point me in the right direction how to parse the file and then display it so that I can easily sepearate each order (header line) and its associated lines (LNE). I did find some code to parse the file, which placed all the items in a one dimensional array but it did not recognise the end of each line with some items being added to the first item of the next line. I should note that the csv file can contain numerous header lines (HDR), with each header containing numerous lines (LNE). I would very much appreciate any help! Many thanks.
 
Probably the easiest way to handle this would be to use the ADO object and query it as if it was a database table. Once you have the data in a recordset you could then convert that to a two dimensional array by using Recordset.GetRows(). This removes your need for having to parse the file and the only major downside is that you will have an extra empty element added to the end of your header rows since they are one element shorter than the line rows.

Somehting like:
Code:
Dim conn, rs_data, arr_data
Set conn = Server.CreateObject("ADODB.Connection")
'open a connection, specifying the path to the folder your CSV file is in
conn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\yourPath\;"

Set rs_data = conn.Execute("SELECT * FROM [YourFileName.csv]")
Set arr_data = rs_data.GetRows()

Set rs_data = Nothing
conn.Close
Set conn = Nothing

Response.Write "Read in " & (UBound(arr_data,2) + 1) & " rows of data"

This code is just an example (and may have some typos) but should clarify what I was saying above if I was a little to vague. hope that helps,

-T

barcode_1.gif
 
Less simple and falls over if there is a comma in a text field but woth looking at
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="csvreader.asp">
  <p>
    <label> 
    <textarea name="csvinput" cols="60" rows="10" id="csvinput"></textarea>
    </label>
  </p>
  <p> 
    <label>
    <input type="submit" name="Submit" value="Submit">
    </label>
  </p>
</form>
<%
function csvReader(csvfile,writeme,heading)
	dim arrCols(),arrRows,arrTable()
	csvinput = csvfile
	style=" style='font-weight:bold;'"
	arrRows = split(csvinput,vbcrlf)
	
	redim arrCols(ubound(arrRows))
	for i = 0 to ubound(arrRows)
		arrCols(i) = split(arrRows(i),",")
	next
	
	redim arrTable(ubound(arrRows),ubound(arrCols))
	csvReader = csvReader + ("<table border=""1"" width=""100%"">"&vbcrlf)
	for i = 0 to ubound(arrRows)
		csvReader = csvReader + ("  <tr>"&vbcrlf)
		for n= 0 to ubound(arrCols(i))
			arrTable(i,n) = arrCols(i)(n)
			if heading and i = 0 then 
				h = style
			else
				h = ""
			end if 
			csvReader = csvReader + ("    <td"&h&">"&arrTable(i,n)&"&nbsp;</td>"&vbcrlf)
		next
		csvReader = csvReader + ("  </tr>"&vbcrlf)
	next
	csvReader = csvReader + ("</table>"&vbcrlf)
	if writeme then
		response.write(csvReader)
	end if
end function
%>
<%
if request("csvinput")<> "" then
	csvReader request("csvinput"),true,true
end if
%>
</body>
</html>

}...the bane of my life!
 
Hi, my appologies for not replying but called away on work. Thank you for both the answers given. I am having a small problem with the getrows method in that it does not seem to pick up the first line in the txt file! The seconds method using the split function does recognise the first line but unfortunately my txt files do contain additional commas. Can you suggest a reason why it is not picking up the first line? Many thanks.
 
Well, I just learned something new :)

Apparently the Text Driver uses some fairly generic settings to read files. You do, on the other hand, have the option of overriding those settings by creating a schema.ini file.

Here is a link to MSDN

Basically your scheme.ini file will look something like:
Code:
[yourFilename.csv]
Format=CSVDelimited
ColNameHeader=False

You don't need to specify the column names, sizes, and types since this isn't a fixed width file. Once you have a schema.ini file set up in the same folder as the csv file it should stop trying to treat the first row of values as the column headers.

-T

barcode_1.gif
 
Worked a treat! Thank you very much I had spent hours tring to work that one out on my own!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top