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

dim conn
dim conn2
set conn=CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source=c:/book1.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=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
wendconn.close
set conn = nothing
conn2.close
set conn2 = nothingfunction 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=localhost;uid=sa;pwd=sa;database=hwtemp;")
sql = "SELECT * into newtable FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source=""c:/book1.xls"";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$] " 
conn.execute(sql) conn.close
set conn = nothing