ASP批量导入Excel到或者Access,Sql Server库中
...
最近在研究批量导入数据的问题,这个其实没有什么难点,只是如果一次性数据全部导入的话,要是数据量小的话,还可以,要是几万条,肯定会出现延时问题。我研究了一下,结合一个朋友的建议,写了一个小导入程序,供大家参考!
<!--#Include File="Conn.asp"-->
<%
Dim ResultNum 定义每次插入记录数
Dim Page 定义循环次数
ResultNum = 100 定义每次插入200条记录
Page = Trim(Request.QueryString("Page"))
If Page = "" Then
Page = 1
End If
Page = Cint(Page)
PageTmp = Page-1 计算当前记录数用
Set Rs = Server.CreateObject("Adodb.RecordSet")
Sql = "Select Count(ID) As Cnt From [Sheet1$]"
Rs.Open Sql,Conn,1,1
Cnt = Rs("Cnt")
Rs.Close
PageSize = Cnt/ResultNum+1 统计总页数
If Page > PageSize Then
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
Response.Write("已经到记录末尾!")
Response.End()
End If
Sql = "Select * From [Sheet1$] Where Id Between "&(PageTmp*ResultNum+1)&" And "&Page*ResultNum&""
Rs.Open Sql,Conn,1,1
If Rs.Eof And Rs.Bof Then
%>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center">暂无商品!</td>
</tr>
</table>
<%
Else
%>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center">正在插入<%=(PageTmp*ResultNum+1)%>条到<%=Page*ResultNum%>条数据,请稍后……</td>
</tr>
</table>
<%
Connstr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source="& Server.MapPath("db/pdlfood.xls")
Set Conn1 = Server.CreateObject("Adodb.Connection")
Conn1.Open Connstr1
Do While Not Rs.Eof
Set Rs1 = Server.CreateObject("Adodb.RecordSet")
Sql1 = "Select * From [Sheet1$] Where FoodCode = "&Rs("FoodCode")&" "
Rs1.Open Sql1,Conn1,1,3
If Rs1.Eof And Rs1.Bof Then
Rs1.AddNew
Rs1("FoodCode") = Rs("FoodCode")
Rs1("FoodName") = Rs("FoodName")
Rs1.Update
Else
不做处理
End If
Rs1.Close
Set Rs1 = Nothing
Rs.MoveNext
Loop
Conn1.Close
Set Conn1 = Nothing
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
Response.Write("<script language=javascript>location.href(?Page="&(Page+1)&");</script>")
%>
上一条:asp实现将excel中的数据批量导入到access
下一条:ASP字符串函数大全