ASP读取CSV文件 并添加到数据库
代码
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
dbpath=Server.MapPath("qipu.mdb")
conn.Open dbpath
strFileName = Request.QueryString("file1")
set rs = Server.CreateObject("ADODB.recordset")
Dim Fso
Dim myFile
Dim titleindex
Dim cateindex
Dim picindex
Dim detailindex
dim arr()
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Set myFile = Fso.OpenTextFile(Server.MapPath("1111.csv"),1,True,-1)
'A.TXT和ASP程序在同个目录下,如果没在一个目录,可以用绝对路径,把上一行的a.txt改为:d:\a.txt
%>
<table border="1">
<%
myNum=0
While Not myFile.AtEndOfStream
If myNum = 0 Then
row=Split(myFile.ReadLine," ")
for i=0 to ubound(row)
MyVar=row(i)
Select Case MyVar
Case "宝贝名称" titleindex= i
Case "宝贝类目" cateindex=i
Case "新图片" picindex=i
End Select
next
End If
row=Split(myFile.ReadLine," ")
%>
<tr>
<%
'for i=0 to ubound(row)
'td=row(i)
title=row(titleindex)
pic=row(picindex)
cate=row(cateindex)
sql="insert into product(title,pic,cate) values('"&title&"','"&pic&"','"&cate&"')"
rs.Open sql, conn
%>
<td ><div style="overflow:hidden; width:200px; height:30px;" ><%=title%></div></td><td ><div style="overflow:hidden; width:200px; height:30px;" ><%=pic%></div></td><td ><div style="overflow:hidden; width:200px; height:30px;" ><%=cate%></div></td>
<%
'next
%>
</tr>
<%
myNum=myNum+1
Wend
myFile.Close
Set myFile = Nothing
Set Fso = Nothing
%>
</table>