ASP怎样将Excel导入到Access里

ASP怎样将Excel导入到Access里,第1张

Excel数据导入到Access、Sql Server中示例代码

将Excel中的数据导入到Access中,前提是在Access中的表已经建好。

dim conn

dim conn2

set conn=CreateObject("ADODB.Connection")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0Jet OLEDB:Database Password=Data Source=c:\book1.mdb"

set conn2=CreateObject("ADODB.Connection")

conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0Jet OLEDB:Database Password=Extended properties=Excel 5.0Data Source=c:\book1.xls"

sql = "SELECT * FROM [Sheet1$]"

set rs = conn2.execute(sql)

while not rs.eof

sql = "insert into xxx([a],[b],[c],[d]) values('"&fixsql(rs(0)) &"','"&fixsql(rs(1)) &"','"&fixsql(rs(2)) &"','"&fixsql(rs(3)) &"')"

conn.execute(sql)

rs.movenext

wend

conn.close

set conn = nothing

conn2.close

set conn2 = nothing

function fixsql(str)

dim newstr

newstr = str

if isnull(newstr) then

newstr = ""

else

newstr = replace(newstr,"'","''")

end if

fixsql = newstr

end function

导入到Sql Server数据库中时,如果Excel文件和数据库不在同一台服务器上时,请参考上面的代码。在同一机器上可以参考下面代码(不需要先把表建表,程序会自己动建表,用Excel中的第一行数据做为表的字段名):

dim conn

set conn=CreateObject("ADODB.Connection")

conn.Open ("driver={SQL Server}server=localhostuid=sapwd=sadatabase=hwtemp")

sql = "SELECT * into newtable FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source=""c:\book1.xls""User ID=AdminPassword=Extended properties=Excel 5.0')...[Sheet1$] "

conn.execute(sql)

conn.close

set conn = nothing

先导出,再导入

Excel导出函数

<%

Sub ExportToExcel

Response.ContentType = "application/vnd.ms-Excel"

Response.AddHeader "Content-Disposition", "attachmentFilename=Results.xls"

Response.Write "<body>"

Response.Write "<table border=1>"

Call WriteTableData

Response.Write "</table>"

Response.Write "</body>"

Response.Write "</html>"

End Sub

%>

Excel导入数据库

<%

dim FileName

FileName="Excel.xls" '取得文件名,来自项目经理的指定,路径固定在某个虚拟路径中

Dim conn, rs

set conn=CreateObject("ADODB.connection")

conn.Open "Driver={Microsoft Excel Driver (*.xls)}" &_

"DriverId=790" &_

"Dbq=" &Server.mappath(""&FileName&"") &"" &_

"DefaultDir=G:\"

set rs=createobject("ADODB.recordset")

rs.Open "Select * from [Sheet1$]",conn, 2, 2

if rs.eof then

response.write "Excel表中无纪录"

else

set connDB = Server.CreateObject("ADODB.Connection")

DBPath = Server.MapPath("Excel.mdb")

'RESPONSE.WRITE DBpath

connDB.Open "driver={Microsoft Access Driver (*.mdb)}dbq=" &DBPath

Set RsDB = Server.CreateObject("ADODB.Recordset")

SQLDB="Select * from Excel"

RsDB.open SQLDB,connDB,1,3

do while not rs.eof '利用循环读出数据

RsDB.addnew

RsDB("filename")=rs(0)

RsDB("id1")=rs(1)

RsDB("id2")=rs(2)

RsDB("id3")=rs(3)

RsDB("id4")=rs(4)

Rs.update

RsDB.movenext

rs.movenext

loop

'response.redirect FileName

end if

RsDB.movefirst

if RsDB.eof then

response.write "数据库中无记录"

else

do while not RsDB.EOF

response.write RsDB("filename")&" "

response.write RsDB("id1")&" "

response.write RsDB("id2")&" "

response.write RsDB("id3")&" "

response.write RsDB("id4")&" "

response.write "<br>"

RsDB.movenext

loop

end if

rs.close

set rs=nothing

set conn=nothing

%>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10034792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存