怎么把EXCEL中的数据自动添加到SQL数据库
2个回答
展开全部
先从一个数据源中读出所有数据,然后通过循环,把源数据插入到我们的目标表中。
完整代码如下:
<%
on error resume next
导入excel电子表格数据到sql sever数据库 by dicky 2004-12-27 16:41:12
function open_conn(sqldatabasename,sqlpassword,sqlusername,sqllocalname,sqlconn)
创建数据库连接对象并打开数据库连接
dim connstr
sql server数据库连接参数:数据库名、用户密码、用户名、连接名(本地用local,外地用ip)
connstr = "provider=sqloledb; user id=" & sqlusername & "; password=" & sqlpassword & "; initial catalog = " & sqldatabasename & "; data source=" & sqllocalname & ";"
set sqlconn = server.createobject("adodb.connection")
sqlconn.open connstr
if err then
err.clear
set sqlconn = nothing
response.write "数据连接错误!"
response.end
end if
end function
function close_conn(sqlconn)
关闭数据库连接并清除数据库连接对象
if isobject(sqlconn) then
sqlconn.close
set sqlconn = nothing
end if
end function
call open_conn("shat_edg","","sa","(local)",sqlconn) 打开本地sql server数据库连接
call open_conn("shat_edg","","sa","ssh03",sqlconn1) 打开远程sql server数据库连接
function get_emp_cnname(ntaccnt)
根据用户nt帐号得到用户中文名
dim sql1,rs1
sql1 = "select emp_cname from rf_employee where emp_ntaccnt="&ntaccnt&""
set rs1 = server.createobject("adodb.recordset")
rs1.open sql1,sqlconn1,1,1
if rs1.eof then
get_emp_cnname = ""
else
get_emp_cnname = rs1("emp_cname")
end if
rs1.close
set rs1 = nothing
end function %>
<html>
<head>
<title>导入excel电子表格数据到sql sever数据库</title>
<body bgcolor="#acd9af">
<center><b>导入excel电子表格数据到sql sever数据库</b></center>
<form method="post" name="form1">
?????源(本地库):
<select name="table" title="请选择需要导入数据的表">
<option></option>
<% dim rssqldatabasetable
set rssqldatabasetable = sqlconn.openschema(20)
do while not rssqldatabasetable.eof %>
<option<%if trim(request("table"))=rssqldatabasetable(2) then response.write " selected"%>><%=rssqldatabasetable(2)%></option>
<% rssqldatabasetable.movenext:loop
set rssqldatabasetable = nothing %>
</select><input type=submit name=submit value="?始?出">
<br>???目?表(远程库):
<select name="table1" title="请选择需要导入数据的表">
<option></option>
<% dim rssqldatabasetable1
set rssqldatabasetable1 = sqlconn1.openschema(20)
do while not rssqldatabasetable1.eof %>
<option<%if trim(request("table1"))=rssqldatabasetable1(2) then response.write " selected"%>><%=rssqldatabasetable1(2)%></option>
<% rssqldatabasetable1.movenext:loop
set rssqldatabasetable1 = nothing %>
</select>
</form>
<font color=blue>?出?程中?不要刷新?面!</font><br>
<%
if trim(request("table1")) <> "" then
dim sql,rs
sql = "select * from sheet1$" sheet1$是我们实现用sql server自身数据转换功能得到的表名
set rs = sqlconn.execute(sql)
if rs.eof and rs.bof then
response.write "没有找到你需要的数据!!<br>"
else
do while not rs.eof
sqlinsert = "insert into "&trim(request("table1"))&" (edg_project_name,edg_project_no,edg_project_vm,edg_project_vm_cnname,edg_project_m,edg_project_m_cnname,edg_project_director,edg_project_director_cnname) values ("&trim(rs("工程名称"))&","&trim(rs("工程编号"))&","&trim(rs("项目副理"))&","&trim(rs("项目副理"))&"("&get_emp_cnname(trim(rs("项目副理")))&")"&","&trim(rs("项目经理"))&","&trim(rs("项目经理"))&"("&get_emp_cnname(trim(rs("项目经理")))&")"&","&trim(rs("项目总监"))&","&trim(rs("项目总监"))&"("&get_emp_cnname(trim(rs("项目总监")))&")"&")"
response.write sqlinsert
此处插入的值根据实际数据库结构进行调整
sqlconn1.execute(sqlinsert)插入excel表格?所有??
rs.movenext
loop
response.write "<font color=red>恭喜,成功?出数据!^_^</font><br>"
end if
rs.close
set rs = nothing
end if
call close_conn(sqlconn) 关闭excel数据库连接
call close_conn(sqlconn1) 关闭sql server数据库连接 %>
</body>
</html>
完整代码如下:
<%
on error resume next
导入excel电子表格数据到sql sever数据库 by dicky 2004-12-27 16:41:12
function open_conn(sqldatabasename,sqlpassword,sqlusername,sqllocalname,sqlconn)
创建数据库连接对象并打开数据库连接
dim connstr
sql server数据库连接参数:数据库名、用户密码、用户名、连接名(本地用local,外地用ip)
connstr = "provider=sqloledb; user id=" & sqlusername & "; password=" & sqlpassword & "; initial catalog = " & sqldatabasename & "; data source=" & sqllocalname & ";"
set sqlconn = server.createobject("adodb.connection")
sqlconn.open connstr
if err then
err.clear
set sqlconn = nothing
response.write "数据连接错误!"
response.end
end if
end function
function close_conn(sqlconn)
关闭数据库连接并清除数据库连接对象
if isobject(sqlconn) then
sqlconn.close
set sqlconn = nothing
end if
end function
call open_conn("shat_edg","","sa","(local)",sqlconn) 打开本地sql server数据库连接
call open_conn("shat_edg","","sa","ssh03",sqlconn1) 打开远程sql server数据库连接
function get_emp_cnname(ntaccnt)
根据用户nt帐号得到用户中文名
dim sql1,rs1
sql1 = "select emp_cname from rf_employee where emp_ntaccnt="&ntaccnt&""
set rs1 = server.createobject("adodb.recordset")
rs1.open sql1,sqlconn1,1,1
if rs1.eof then
get_emp_cnname = ""
else
get_emp_cnname = rs1("emp_cname")
end if
rs1.close
set rs1 = nothing
end function %>
<html>
<head>
<title>导入excel电子表格数据到sql sever数据库</title>
<body bgcolor="#acd9af">
<center><b>导入excel电子表格数据到sql sever数据库</b></center>
<form method="post" name="form1">
?????源(本地库):
<select name="table" title="请选择需要导入数据的表">
<option></option>
<% dim rssqldatabasetable
set rssqldatabasetable = sqlconn.openschema(20)
do while not rssqldatabasetable.eof %>
<option<%if trim(request("table"))=rssqldatabasetable(2) then response.write " selected"%>><%=rssqldatabasetable(2)%></option>
<% rssqldatabasetable.movenext:loop
set rssqldatabasetable = nothing %>
</select><input type=submit name=submit value="?始?出">
<br>???目?表(远程库):
<select name="table1" title="请选择需要导入数据的表">
<option></option>
<% dim rssqldatabasetable1
set rssqldatabasetable1 = sqlconn1.openschema(20)
do while not rssqldatabasetable1.eof %>
<option<%if trim(request("table1"))=rssqldatabasetable1(2) then response.write " selected"%>><%=rssqldatabasetable1(2)%></option>
<% rssqldatabasetable1.movenext:loop
set rssqldatabasetable1 = nothing %>
</select>
</form>
<font color=blue>?出?程中?不要刷新?面!</font><br>
<%
if trim(request("table1")) <> "" then
dim sql,rs
sql = "select * from sheet1$" sheet1$是我们实现用sql server自身数据转换功能得到的表名
set rs = sqlconn.execute(sql)
if rs.eof and rs.bof then
response.write "没有找到你需要的数据!!<br>"
else
do while not rs.eof
sqlinsert = "insert into "&trim(request("table1"))&" (edg_project_name,edg_project_no,edg_project_vm,edg_project_vm_cnname,edg_project_m,edg_project_m_cnname,edg_project_director,edg_project_director_cnname) values ("&trim(rs("工程名称"))&","&trim(rs("工程编号"))&","&trim(rs("项目副理"))&","&trim(rs("项目副理"))&"("&get_emp_cnname(trim(rs("项目副理")))&")"&","&trim(rs("项目经理"))&","&trim(rs("项目经理"))&"("&get_emp_cnname(trim(rs("项目经理")))&")"&","&trim(rs("项目总监"))&","&trim(rs("项目总监"))&"("&get_emp_cnname(trim(rs("项目总监")))&")"&")"
response.write sqlinsert
此处插入的值根据实际数据库结构进行调整
sqlconn1.execute(sqlinsert)插入excel表格?所有??
rs.movenext
loop
response.write "<font color=red>恭喜,成功?出数据!^_^</font><br>"
end if
rs.close
set rs = nothing
end if
call close_conn(sqlconn) 关闭excel数据库连接
call close_conn(sqlconn1) 关闭sql server数据库连接 %>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |