如何用asp读取Excel文件
1个回答
2015-11-12 · 知道合伙人数码行家
关注
展开全部
下面是读取一个EXCEL文件并将其写入数据库的代码实例,亲测通过:
注意:EXCEL里面的列名称要和数据库里的字段相同
<%
i=0
Dim cn,oConn,connstr
'打开XLS.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "Microsoft.Jet.OLEDB.4.0 "
cn.ConnectionString = "Data Source=" & Server.MapPath("list.xls") & ";" & _
"Extended Properties=Excel 8.0;"
cn.Open
'打开MDB.
connstr="DBQ="+server.MapPath("TEST.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set oConn=server.CreateObject("ADODB.CONNECTION")
oConn.open connstr
'读取数据.
set rsRead=server.CreateObject("ADODB.Recordset")
rsRead.Open "select * from [list$]",cn,1,1
do until rsRead.EOF
'写入数据库.
oConn.Execute("Insert into eer(mc)Values('"& rsRead.Fields("mc") & "')" )
rsRead.MoveNext
i=i+1
loop
response.write("<p align=center>成功导入"&i&"条数据</p><br>")
response.Write("<p align=center><a href=javascript:window.close()>关闭窗口</a></p>")
%>
注意:EXCEL里面的列名称要和数据库里的字段相同
<%
i=0
Dim cn,oConn,connstr
'打开XLS.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "Microsoft.Jet.OLEDB.4.0 "
cn.ConnectionString = "Data Source=" & Server.MapPath("list.xls") & ";" & _
"Extended Properties=Excel 8.0;"
cn.Open
'打开MDB.
connstr="DBQ="+server.MapPath("TEST.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set oConn=server.CreateObject("ADODB.CONNECTION")
oConn.open connstr
'读取数据.
set rsRead=server.CreateObject("ADODB.Recordset")
rsRead.Open "select * from [list$]",cn,1,1
do until rsRead.EOF
'写入数据库.
oConn.Execute("Insert into eer(mc)Values('"& rsRead.Fields("mc") & "')" )
rsRead.MoveNext
i=i+1
loop
response.write("<p align=center>成功导入"&i&"条数据</p><br>")
response.Write("<p align=center><a href=javascript:window.close()>关闭窗口</a></p>")
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |