asp中怎么导入excel到SQL中?

如题所述

Set db = Server.CreateObject("ADODB.Connection")
db.Open "Driver={Microsoft Excel Driver (*.xls)};Dbq=" & Server.MapPath(FileName)
'打开记录集,表名一定要以"[表名$]"的格式
strSql="Select * From [Sheet1$]"
Set rsexcel=db.Execute(strSql)
Set rs = Server.CreateObject("ADODB.RecordSet")
SQL = "Select * From [users]"
rs.Open SQL, Conn, 1, 3
N = rs.fields.count-1'数据表字段数
M = rsexcel.fields.count-1'excel表里字段数
Redim L(N)'定义数据表数组
Redim arr(M)'定义excel表数组

'把数据表里的字段表存入数组L中
For k=0 To N
L(k) = Rs(k).name
next
'把excel表里的字段存入数组arr中
for p=0 to M
arr(p) = rsexcel(p).name
next
'取数组长度
x=UBound(l)
y=UBound(arr)

j=0
startime=timer() '开始时间
Do While Not rsexcel.EOF

rs.AddNew
'循环读取所有行
for i=6 to x'对数据表进行循环 rs(0)是 id 所以跳过
if l(i)=arr(j) then'通过数组判断两表字段是否相同,如相同对其进行赋值,不相同置0
if Trim(rsexcel(j))<>"" then
'response.write Trim(rsexcel(j))&"|"
rs(i)=Trim(rsexcel(j))
else
rs(i)=0
end if
if j< M then '当execl表数据是最后一项时不能在加1了
j=j+1
else
j=j
end if
else
rs(i)=0
end if
rs(3)=typeid
Next
rsexcel.MoveNext
rs.update
j=0
Loop
endtime=timer()
'关闭对象
rsexcel.Close
Set rsexcel=nothing
db.Close
Set db=Nothing
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-26
我没有写过asp,但是初步思路应该是asp打开excel文件,然后写代码逐行、逐块等各种方式读入数据,然后用sql语句insert或者update到数据库中追问

这个我知道啊,我就不知道怎么写。

追答

对不起,我也没有写过,asp应该有类似openfile之类的函数取得一个句柄这类的,找专门书籍看看吧