日期:2014-05-17  浏览次数:20882 次

asp如何上传excel并导入sql server 2005?
谢谢

------解决方案--------------------
思路是这样

1 先上传 asp上传的代码到处都是 我就不提供了
2 读取 用个循环语句 写入数据库
打开execl的方法
Dim Conn,Driver,DBPath,Rs,iyear,imonth,DBname
DBname = "已上传文件名.xls"
'' 建立Connection对象
Set Conn = Server.CreateObject("ADODB.Connection")
Driver = "Driver={Microsoft Excel Driver (*.xls)};"
DBPath = "DBQ=" & Server.MapPath( ""&DBname&"" )
''调用Open 方法打开数据库
Conn.Open Driver & DBPath
''注意 表名一定要以下边这种格试 "[表名$]" 书写
Sql="Select * From [Sheet1$]"
Set Rs=Conn.Execute(Sql)



注意:服务器上装excel


------解决方案--------------------
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/>
<%

'sql 2005数据库,请用下面 连接驱动
connstr = "Driver={SQL Server};server=127.0.0.1;uid=sa;pwd=;database=dmxx;"
dim aa
aa="excel地址"
'Excel连接驱动
aaa="provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath(aa)&";Extended Properties=Excel 8.0"
set StrConnect=CreateObject("ADODB.Connection")
StrConnect.Open aaa

Set objConn=CreateObject("ADODB.Connection")
objConn.Open connstr

'注意 表名一定要以下边这种格试 "[表名$]" 书写
Set rs = Server.CreateObject("ADODB.Recordset")
Sql="select * from [sheet1$]"
rs.Open Sql,StrConnect,2,2
IF rs.Eof And Rs.Bof Then
 response.Write("<script>alert('没有资料可以导入!');history.go(-1)</script>")
else
Do While Not rs.EOF 
sql="select * from sdb_members"

Set rssql = Server.CreateObject("ADODB.Recordset")
rssql.Open sql,conn,1,3

rssql.AddNew 
if not rssql.eof then
'向SQL数据库中写数据
 if rs(0)<>"" then
rssql("uname")=rs(0)
 end if
rssql.Update
end if
Rs.MoveNext
Loop
rs.close
end if
response.Write "<script language=javascript>alert('资料导入成功!');history.go(-1)</script>" 
%>