作者:乔山办公网日期:
返回目录:excel表格制作
先从一个数据源中读出所有数据,然后通过循环,把源数据插入到我们的目标表中。
完整代码如下:
<%
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数据库连接参数:数据库名e79fa5e98193e59b9ee7ad94365、用户密码、用户名、连接名(本地用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="?始?出">
???目?表(远程库):
<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>
<%
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 "<font color=red>恭喜,成功?出数据!^_^</font>"
end if
rs.close
set rs = nothing
end if
call close_conn(sqlconn) 关闭excel数据库连接
call close_conn(sqlconn1) 关闭sql server数据库连接 %>
</body>
</html>
1、首先打开SQL Server数据库,准备一个要导入的数据表,如下图所示百,数据表中插入一些数据
2、接下来度打开Excel软件,点击数据问菜单,然后选择其他来源下面的来自SQL Server选项
3、然后会弹出数据库连接向导答界面,在输入框里面输入服务器名称,也就是计算机名
4、接下来就会回出现导入内容的具体设置界面了,我们选择要导入表,并且选择一个空白单元格位置作为数据的导入位置
5、最后点击确定按钮,我们就可以看到SQL Server中的数据就导入进Excel里了答
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\temp1.xls";User ID=;Password=;Extended properties=Excel 5.0')...[结果$]
http://apps.hi.baidu.com/share/detail/18394176
看下知这道个专属