import csv data line by line with ASP

Please support our ASP advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Oct 2006
Posts: 19
Reputation: doraima29 is an unknown quantity at this point 
Solved Threads: 0
doraima29 doraima29 is offline Offline
Newbie Poster

import csv data line by line with ASP

 
0
  #1
Dec 14th, 2007
How to import CSV data line by line with ASP?
I wanted this code to import data line by line after a form has been submitted

I'm using a Classic ASP to code all of the dynamic stuff

I have this code--

<title>Import CSV to HTML</title>
<body bgcolor="#FFFFFF">
<%
csv_to_read="states.csv"
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(csv_to_read))

imported_text = act.readline
'Read the first line of the csv, typically these are colum headings

imported_text = replace(imported_text,chr(13),",")
'Change the line breaks to commas to delimit through-out

imported_text = replace(imported_text,chr(34),"")
'Remove al quotes (If your csv has quotes other than to seperate text
'You may want to remove this modifier to the imported text

split_text=split(imported_text,",")
'Split the top line by comma

num_imported=ubound(split_text)+1
'Count the number of splits and add one for the last element

total_imported_text = act.readall
'Read the rest of the csv

total_imported_text = replace(total_imported_text,chr(13),",")
'Change the line breaks to commas to delimit through-out

total_imported_text = replace(total_imported_text,chr(34),"")
'Remove al quotes (If your csv has quotes other than to seperate text
'You may want to remove this modifier to the imported text

total_split_text=split(total_imported_text,",")
'Split the file up by comma

total_num_imported=ubound(total_split_text)
'Count the number of splits
'This will be the numer of cells in the table
%>
<table width="100%">
<tr>
<%
for table = 0 to num_imported -1
'This will create a table cell for each column in the csv
' (-1 is used because arrays begin with 0)
%>
<td width="<% response.write 100/(num_imported) 'make the cell widths even %>%">
<b><%= split_text(count) %></b>
</td>
<%
count=count+1
next
%>
</tr>
<tr>
<%
'Reset the counter
count=0
' This wil determine how many rows are in the csv
for tablea = 0 to (total_num_imported/ (num_imported)-1)
%>
<%
for table = 0 to num_imported -1
'This will create a table cell for each column in the csv
' (-1 is used because arrays begin with 0)
%><td width="<%= 100/(num_imported) %>%">
<%= total_split_text(count)
%>
</td>
<%
count=count+1
next ' end of the observation
%></tr>
<% next 'end of the csv %>
</table>


<%@ Language=VBScript %>

<%
Option Explicit
%>

<%
' -----------------------------------------------------
' CDONTS Email send script
' © http://www.designplace.org/
' Comments must remain intact for re-use of this code
' -----------------------------------------------------

dim strName, strEmailOne, strEmailTwo, strMessage, optSuggestions, strEmailTo

strName = Request.Form("name") ' holds inputted name
'strEmailOne = Request.Form("emailOne") ' holds inputted email address
strEmailTwo = Request.Form("emailTwo") ' holds inputted email address
strMessage = Request.Form("message") ' holds inputted message
optSuggestions = Request.Form("suggestions").Item ' drop down list selection

' -- check all fields for empty values --
' -- remove and add new as required --

if strMessage = "" then
Response.Redirect "suggestion_box.asp?action=err4"
end if

' if strName = "" then
' Response.Redirect "suggestion_box.asp?action=err1"
' else if strEmailOne = "" then
' Response.Redirect "suggestion_box.asp?action=err2"
' else if strEmailTwo = "" then
'Response.Redirect "suggestion_box.asp?action=err3"
'else if strMessage = "" then
'Response.Redirect "suggestion_box.asp?action=err4"
'else if optSuggestions = "" then
'Response.Redirect "suggestion_box.asp?action=err5"
'end if
'end if
'end if
'end if
'end if

' -- start determine correct send to address based on suggestion type --

Select Case optSuggestions

Case "Web Enhancements"
strEmailTo = "person5@optIT.com"

Case "Application Enhancements"
strEmailTo = "person1@optIT.com;person2@optIT.com"

Case "New Product Suggestions"
strEmailTo = "person1@optIT.com;person2@optIT.com"

Case "Workplace Suggestions"
strEmailTo = "person1@optIT.com;person2@optIT.com"

Case "Employee Recognition"
strEmailTo = "person3@optIT.com;person4@optIT.com"

Case "Cultural Events"
strEmailTo = "person3@optIT.com;person4@optIT.com"

Case "Other"
strEmailTo = "person3@optIT;person4@optIT.com"

Case Else
strEmailTo = "feedback@optIT.com"

End Select

' -- end determine correct send to address based on suggestion type --

' -- begin email send process --

dim objMail

Set objMail = Server.CreateObject("CDO.Message")
'Set objMail = CreateObject("CDONTS.NewMail")

' -- email variables --
objMail.From = Trim(""person3@optIT.com;person4@optIT.com")
objMail.To = Trim(strEmailTo)
objMail.Subject = "Educators Program"
'objMail.BodyFormat = "0" ' HTML format
objMail.TextBody = "Your Discount Code from Educators Program:" & Trim(dsctCodes) & vbCrLf _
& vbCrLf _
& "Question1: " & Trim(optSuggestions) & vbCrLf _
& vbCrLf _
& "Message: " & Trim(strMessage)

' -- send the email --
objMail.Send()

' -- clean up object
Set objMail = Nothing

' -- execute confirmation page
Response.Redirect "thankyou.asp"
%>
AddThis Social Bookmarking Widget
Reply With Quote
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: import csv data line by line with ASP

 
0
  #2
Dec 17th, 2007
What exactly are you trying to do. The email code does not make sense. The only bug on your code that imports the csv information is that it never writes the last line of the csv file to the browser. You need to change:

total_num_imported=ubound(total_split_text)
to:
total_num_imported=ubound(total_split_text)+1

Here is how I would do it:
  1. <%@language="vbscript"%>
  2. <table border="1">
  3. <%
  4. 'dim csv_to_read, fso, act, imported_text,split_text, total_imported_text,total_split_text,total_num_imported
  5. dim csv_to_read,counter,line,fso,objFile
  6.  
  7. csv_to_read="states.csv"
  8. counter=0
  9. set fso = createobject("scripting.filesystemobject")
  10. set objFile = fso.opentextfile(server.mappath(csv_to_read))
  11.  
  12. Do Until objFile.AtEndOfStream
  13. line = split(objFile.ReadLine,",")
  14. counter=counter + 1
  15.  
  16. Response.Write "<tr>"
  17. if counter > 1 Then
  18. for i=0 to ubound(line)
  19. Response.Write "<td>"&line(i)&"</td>"
  20. next
  21. else
  22. for i=0 to ubound(line)
  23. Response.Write "<th>"&line(i)&"</th>"
  24. next
  25. end if
  26. Response.Write "</tr>" & chr(13)
  27. Loop
  28. objFile.Close
  29. %><caption>Total Number of Records: <%=counter-1%></caption>
  30. </table>
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: import csv data line by line with ASP

 
0
  #3
Dec 17th, 2007
Here is shorter alternative:
  1. <%@language="vbscript"%>
  2. <table border="1">
  3. <%
  4. dim csv_to_read, fso, act
  5.  
  6. csv_to_read="states.csv"
  7. set fso = createobject("scripting.filesystemobject")
  8. set act = fso.opentextfile(server.mappath(csv_to_read))
  9.  
  10. 'Read the first line of the csv, typically these are colum headings
  11. Response.Write "<tr><th>" & replace(act.readline,",","</th><th>") & "</th></tr>" & vbCrLf
  12.  
  13. 'Read the rest of the csv
  14. Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
  15. %>
  16. <caption>Total Number of Records: <%=act.Line-1%></caption>
  17. </table>

BTW, my states.csv looks as follows:
  1. name,abbreviation
  2. Illinois,IL
  3. Indiana,IN
  4. Ohio,OH
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC