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