ASP里,將Excel導(dǎo)入到Access數(shù)據(jù)庫
...
用ASP編的一個網(wǎng)站,數(shù)據(jù)庫是Access,數(shù)據(jù)庫名稱是atest,里面有個表格user,user表格有user和password兩個字段現(xiàn)要將Excel表格(名稱是test.xls)的數(shù)據(jù)導(dǎo)入到atest數(shù)據(jù)庫的user表中,怎么實現(xiàn)?高手支招
............................................................
下面連接到test.xls表sheet1$ 表后面要加個$的,一定要加這個符號
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Excel Driver (*.xls)};ReadOnly=0;DBQ=" & Server.MapPath("test.xls")
SQL1="select * from [sheet1$]"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open SQL1, conn, 3, 3
下面連接到atest.mdb表user
curDir = Server.MapPath("atest.mdb")
Set conn1 = Server.CreateObject("ADODB.Connection")
conn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & curDir
Set rs1 = Server.CreateObject("ADODB.Recordset")
Set rs1.ActiveConnection = conn1
rs1.Source = "select * from user"
rs1.CursorType = 3 adOpenKeyset
rs1.LockType = 3 adLockOptimistic
rs1.Open
Do While Not rs.Eof
rs1.AddNew
rs1(0)=j
for i=0 to rs.Fields.Count-1
rs1(i)=Trim(rs(i))
Next
rs1.Update
rs.MoveNext
j=j+1
Loop
rs.Close
rs1.Close
conn.Close
conn1.Close
Set rs=nothing
Set conn=nothing
............................................................................
在ASP中怎樣把EXCEL導(dǎo)入ACCESS
demo.asp
<%
Dim cn,oConn,connstr
打開XLS.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "Microsoft.Jet.OLEDB.4.0 "
cn.ConnectionString = "Data Source=" & Server.MapPath("data.xls") & ";" & _
"Extended Properties=Excel 8.0;"
cn.Open
打開MDB.
connstr="DBQ="+server.MapPath("data.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set oConn=server.CreateObject("ADODB.CONNECTION")
oConn.open connstr
讀取數(shù)據(jù).
set rsRead=server.CreateObject("ADODB.Recordset")
rsRead.Open "select * from [Sheet1$]",cn,1,1
do until rsRead.EOF
寫入數(shù)據(jù)庫.
oConn.Execute("Insert into users(userid,password)Values("& rsRead.Fields("userid")&"," &rsRead.Fields("password") & ")" )
rsRead.MoveNext
loop
%>
數(shù)據(jù)庫data.mdb
表users
id,uesrid,password
Excel文件data.xls
數(shù)據(jù)
userid password
wang w521
zhange z254
hong h112
================!!!!ASP讀取EXCEL注事項!!!!======================
i)將Excel97或Excel2000生成的XLS文件(book)看成一個數(shù)據(jù)庫,其中的每一個工作表(sheet)看成數(shù)據(jù)庫表
ii)ADO假設(shè)Excel中的第一行為字段名.所以你定義的范圍中必須要包括第一行的內(nèi)容
iii)Excel中的行標題(即字段名)不能夠包含數(shù)字. Excel的驅(qū)動在遇到這種問題時就會出錯的。例如你的行標題名為“F1”
iiii)如果你的Excel電子表格中某一列同時包含了文本和數(shù)字的話,那么Excel的ODBC驅(qū)動將不能夠正常, 處理這一行的數(shù)據(jù)類型,你必須要保證該列的數(shù)據(jù)類型一致
====================================================
上一條:ASP批量導(dǎo)入Excel到或者Access,Sql Server庫中
下一條:ASP字符串函數(shù)大全