asp上传excle文件并导入到access数据库

<%

function FSOFileDel(filename)

Dim objFSO,objCountFile,FiletempData

On Error Resume Next

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Set objCountFile =objFSO.DeleteFile(Server.MapPath(filename),true)

objCountFile.Close

Set objCountFile=Nothing

Set objFSO = Nothing

End Function








dim action,files

action=trim(request("action"))

files=trim(request("files"))

select case action

case "add"

call add(files)

case else

call default()

end select        

%>



<%sub default()%>

<form action="?action=add" method="post" id="form1" name="form1">

<table border="0" cellspacing="0" cellpadding="0">

<tr class="title2">

<td colspan="2">批量上传</td>

</tr>

<tr>

<td>1.上传Excle文件&nbsp;</td>

<td><input name="files" type="hidden" id="files" />

<iframe frameborder="0" scrolling="no" height="25" width="400" src="upload.asp?action=add"></iframe>&nbsp;</td>

</tr>

<tr>

<td>2.点击"开始上传"&nbsp;</td>

<td>  <label>

<input type="submit" name="button" id="button" value="开始上传" />

</label>&nbsp;</td>

</tr>

</table>







</form>

<%end sub%>

<%

sub add(files)

dim FileName



if files="" then ClientAlert"请先上传Excle文件","?"



FileName="../upload/file/"&files  '取得文件路径

Dim connEX

set connEX=CreateObject("ADODB.connection")

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

"DriverId=790;" & _

"Dbq=" & server.mappath(""&FileName&"") & ";" & _

"DefaultDir=G:" 



set rs=createobject("ADODB.recordset")

rs.Open "Select * From [Sheet1$]",connEX, 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 product"

RsDB.open sqlDB,conn,1,3

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

RsDB.addnew

RsDB("ProName")=rs(0)

RsDB("ProClassId")=rs(1)

RsDB("ProPics")=rs(2)

RsDB("ProPic")=rs(3)

RsDB("ProFeaturesCn")=rs(4)

RsDB("IsNew")=rs(5)

RsDB("IsHot")=rs(6)

RsDB("Taxis")=rs(7)

Rs.update

RsDB.movenext

rs.movenext

loop

'response.redirect FileName

end if

RsDB.movefirst

if RsDB.eof then

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

end if

rs.close

set rs=nothing

set connEX=nothing



Call FSOFileDel("../upload/file/"&files)'导入成功后及时删除上传文件.



ClientAlert"导入成功,点确定继续。","?"

end sub
 

%>

未经允许不得转载:445IT之家 » asp上传excle文件并导入到access数据库

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏