ASP中将Excel数据导入到Access
<% dim conn '定义一个连接变量 dim conn2 '定义第二个连接变量 'On Error Resume Next Server.ScriptTimeOut = 999999 '超时时间 set conn=CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source="&Server.MapPath("Fang.mdb") '要导入的数据库名称,这里是Fang.mdb
set conn2=CreateObject("ADODB.Connection") conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source="&Server.MapPath("Fang.xls") '要导入的EXCEL表名称Fang.xls '----------------------------------------------------------------------------------------------------------------- '这里要说明的是,数据库和Excel表和该文件必须在网站同一目录里 '----------------------------------------------------------------------------------------------------------------- sql = "SELECT * FROM [Sheet1$]" '要导入的Excel数据里面的表的名称,后面一定要加$ set rs = conn2.execute(sql) while not rs.eof sql = "insert into zhenya([country],[mobile]) values('"& fixsql(rs(0)) &"','"& fixsql(rs(1)) &"')" '其中的zhenya是要导入的目标表,其后的country,mobile是zhenya表中的字段名,但是要和后面的fixsql(rs(数字))要对应! 'response.write sql 'response.end conn.execute(sql) rs.movenext Response.Write "正在插入 "&sql&"<Br>" '在这里输出执行的语句,可以去掉的! Response.Flush wend conn.close set conn = nothing conn2.close set conn2 = Nothing
If Err = 0 Then Response.Write "导入成功" Else Response.Write "导入失败!" End If
function fixsql(str) dim newstr newstr = str if isnull(newstr) then newstr = "" else newstr = replace(newstr,"'","''") end if fixsql = newstr end Function %>
|