完整代码如下:
<%
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 %>
導出過程中請不要刷新頁面!
<%
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 "没有找到您需要的数据!!
"
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 "恭喜,成功導出数据!^_^
"
end if
rs.close
set rs = nothing
end if
call close_conn(sqlconn) 关闭excel数据库连接
call close_conn(sqlconn1) 关闭sql server数据库连接 %>
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。