完整代码如下:
<%
'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